馬上加入Android 台灣中文網,立即免費下載應用遊戲。
您需要 登錄 才可以下載或查看,沒有帳號?註冊
x
我的伺服器是php
手機上傳圖片時小圖可以上傳 大圖超過2.5M的無法上傳
以下是大圖無法上船的log
08-11 22:33:38.810: D/SensorManager(19016): registerListener :: handle = 0 name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@425b6a30
08-11 22:33:41.605: D/Create Response(19016): {"message":"Product successfully created.","success":1}
08-11 22:33:41.750: D/SensorManager(19016): unregisterListener:: Listener= android.view.OrientationEventListener$SensorEventListenerImpl@425b6a30
08-11 22:33:41.750: D/Sensors(19016): Remain listener = Sending .. normal delay 200ms
08-11 22:33:41.750: I/Sensors(19016): sendDelay --- 200000000
08-11 22:33:41.750: D/SensorManager(19016): JNI - sendDelay
08-11 22:33:41.750: I/SensorManager(19016): Set normal delay = true
片段程式碼如下- int res=0;
- String result = null;
- String BOUNDARY = UUID.randomUUID().toString(); // 边界标识 随机生成
- String PREFIX = "--", LINE_END = "\r\n";
- String CONTENT_TYPE = "multipart/form-data"; // 内容类型
- try {
- URL url = new URL(strUrl);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- // conn.setReadTimeout(TIME_OUT);
- // conn.setConnectTimeout(TIME_OUT);
- conn.setDoInput(true); // 允许输入流
- conn.setDoOutput(true); // 允许输出流
- conn.setUseCaches(false); // 不允许使用缓存
- conn.setRequestMethod("POST"); // 请求方式
- conn.setRequestProperty("Charset", CHARSET); // 设置编码
- conn.setRequestProperty("connection", "keep-alive");
- conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary="+ BOUNDARY);
- if (file != null) {
- /**
- * 当文件不为空时执行上传
- */
- DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
- StringBuffer sb = new StringBuffer();
- sb.append(PREFIX);
- sb.append(BOUNDARY);
- sb.append(LINE_END);
- /**
- * 这里重点注意: name里面的值为服务器端需要key 只有这个key 才可以得到对应的文件
- * filename是文件的名字,包含后缀名
- */
- sb.append("Content-Disposition: form-data; name=\"myfile\"; filename=\""
- + imagename + "\"" + LINE_END);
- sb.append("Content-Type: application/octet-stream; charset="
- + CHARSET + LINE_END);
- sb.append(LINE_END);
- dos.write(sb.toString().getBytes());
- InputStream is = new FileInputStream(file);
- int bytesAvailable = is.available();
- int bufferSize = Math.min(bytesAvailable, 1024 * 1024 * 1024*1024);
- byte[] bytes = new byte[bufferSize];
- int len = 0;
- while ((len = is.read(bytes)) != -1) {
- dos.write(bytes, 0, len);
- }
- is.close();
- dos.write(LINE_END.getBytes());
- byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END)
- .getBytes();
- dos.write(end_data);
- dos.flush();
- /**
- * 获取响应码 200=成功 当响应成功,获取响应的流
- */
- res = conn.getResponseCode();
- if (res == 200) {
- Log.e(TAG, "request success");
- InputStream input = conn.getInputStream();
- StringBuffer sb1 = new StringBuffer();
- int ss;
- while ((ss = input.read()) != -1) {
- sb1.append((char) ss);
- }
- result = sb1.toString();
- Log.e("ok", "result : " + result);
- } else {
- Log.e(TAG, "request error");
- }
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
複製代碼 |