【发布时间】:2017-01-10 04:51:20
【问题描述】:
我有这个问题,我想通过多部分表单将图像上传到服务器上,但是当我尝试从 android 上传但从 web 可以正常工作时,它一直给我无效的图像。 这是可以正常工作的 html 表单。
http://letitripple.org/htmlForm.html
这是这个 html 表单创建的请求。 (我从 chrome 开发者工具中获得)
------WebKitFormBoundaryPug6xAUAlaUPbR86 内容处置:表单数据; name="wp-user-avatars";文件名="沙漠.jpg" 内容类型:image/jpeg
------WebKitFormBoundaryPug6xAUAlaUPbR86 内容处置:表单数据; name="login_id"
51 ------WebKitFormBoundaryPug6xAUAlaUPbR86 内容处置:表单数据;名称="cookie"
ali.asim@gmail.com|1473394633|vcN3CTbKi6pkAjPMKH1D9MHucPLKhw3wyeS7ViSTgGb|f5cbd913f6c3d6940066a660bac5908adb016682ce34f48e8200773eb4503c1e ------WebKitFormBoundaryPug6xAUAlaUPbR86 内容处置:表单数据;名称="提交"
提交 ------WebKitFormBoundaryPug6xAUAlaUPbR86--
这是我的安卓代码,它不工作。
public AppResponse uploadImageFile(String csURL, String csFilePath)
{
AppResponse appResponse = null;
HttpURLConnection conn = null;
DataOutputStream dos = null;
ByteArrayOutputStream output = null;
InputStream inStream = null;
String Url = csURL;
String existingFileName = csFilePath;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "WebKitFormBoundaryleA9RIQsQBxW8Cgl";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
// String reponse_data = null;
int maxBufferSize = 1 * 1024 * 1024;
// String urlString = "YOUR PHP LINK FOR UPLOADING IMAGE";
try {
//------------------ CLIENT REQUEST
// FileInputStream fileInputStream = new FileInputStream(new File(existingFileName) );
// open a URL connection to the Servlet
URL url = new URL(Url);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
FileInputStream fileInputStream = new FileInputStream(new File(existingFileName));//You can get an inputStream using any IO API
byte[] bytes;
byte[] buffers = new byte[605244];
int bytesReads;
output = new ByteArrayOutputStream();
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + twoHyphens + twoHyphens + boundary);
//conn.setRequestProperty("attach1", existingFileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + twoHyphens + twoHyphens + boundary + "\r\n");
dos.writeBytes("Content-Disposition: form-data; name=\"attach1\"; filename=\"profile.jpg\"\n Content-Type: image/jpg\r\n\r\n\r\n"); // uploaded_file_name is the Name of the File to be uploaded
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
Log.e("Value", "Writing");
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + twoHyphens + twoHyphens + boundary + twoHyphens);
//String strInput = dos.toString();
int responseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
fileInputStream.close();
dos.flush();
dos.close();
int nResponseCode = conn.getResponseCode();
inStream = new DataInputStream(conn.getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(inStream));
String line = "";
StringBuilder csResult = new StringBuilder("");
while ((line = in.readLine()) != null) {
Log.e("Debug", "Server Response " + csResult);
csResult.append(line);
}
appResponse = new AppResponse(nResponseCode, csResult.toString());
}
catch (IOException ioe) {
Log.e("Debug", "error: " + ioe.getMessage(), ioe);
}
return appResponse;
}
【问题讨论】:
-
我不确定my case 是否有帮助... FYR
标签: android multipartform-data