我通过Android 的urlConnection去上传文件,但是Spring MVC却接收不到,但是使用
jodd http库就可以。
常数代码:
private static final String END = "\r\n";
private static final String PREFIX = "--";
private static final String BOUNDARY = "***************";
private static final String REQUEST_POST = "POST";
private static final int TIME_OUT = 5000;
private static final String PROPERTY_KEY_CONNECTION = "Connection";
private static final String PROPERTY_KEY_CHARSET = "Charset";
private static final String PROPERTY_KEY_COOKIE = "Cookie";
private static final String PROPERTY_KEY_CONTENT_TYPE = "Content-Type";
private static final String PROPERTY_VALUE_CONNECTION = "Keep-Alive";
private static final String PROPERTY_VALUE_CHARSET = "UTF-8";
private static final String PROPERTY_VALUE_REQUEST_CONTENT_TYPE = "multipart/json";
private static final String PROPERTY_VALUE_FILE_UPLOAD_CONTENT_TYPE = "multipart/form-data;boundary=" + BOUNDARY;
private static final String PROPERTY_KEY_ACCEPT = "Accept";
private static final String PROPERTY_VALUE_ACCEPT = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
自己定义的文件上传:
//这里是之前我上传用的方法
try {
mURL = new URL(FeedbackConst.FeedbackUrl.URL_MAIN + FeedbackConst.FeedbackUrl.URL_SUB_UPLOAD);
LogUtils.i(FeedbackConst.FeedbackUrl.URL_MAIN + FeedbackConst.FeedbackUrl.URL_SUB_UPLOAD);
mHttpURLConnection = (HttpURLConnection) mURL.openConnection();
//set properties
mHttpURLConnection.setDoInput(true);
mHttpURLConnection.setDoOutput(true);
mHttpURLConnection.setUseCaches(false);
String sessionId = getSessionIdFromSQLite();
mHttpURLConnection.setRequestProperty(PROPERTY_KEY_COOKIE, sessionId);
mHttpURLConnection.setRequestMethod(REQUEST_POST);
mHttpURLConnection.setRequestProperty(PROPERTY_KEY_CONNECTION, PROPERTY_VALUE_CONNECTION);
mHttpURLConnection.setRequestProperty(PROPERTY_KEY_CHARSET, PROPERTY_VALUE_CHARSET);
mHttpURLConnection.setRequestProperty(PROPERTY_KEY_CONTENT_TYPE, PROPERTY_VALUE_FILE_UPLOAD_CONTENT_TYPE);
mHttpURLConnection.addRequestProperty(PROPERTY_KEY_ACCEPT, PROPERTY_VALUE_ACCEPT);
DataOutputStream uploadFileOutputStream = new DataOutputStream(mHttpURLConnection.getOutputStream());
int length;
Iterator<String> filenames = file.keys();
String filename = filenames.next();
LogUtils.i("file name : " + filename);
String filePath = file.getString(filename);
LogUtils.i("file path : " + filePath);
File uploadFile = new File(filePath);
FileInputStream fileInputStream = new FileInputStream(uploadFile);
//DataInputStream dataInputStream = new DataInputStream(fileInputStream);
LogUtils.i(LOG_TAG, "here is running!");
if (null == filename) {
filename = filePath.substring(filePath.lastIndexOf("/") + 1);
}
//here may be is wrong
uploadFileOutputStream.writeBytes(END + PREFIX + BOUNDARY + PREFIX + END);
uploadFileOutputStream.writeBytes("Content-Disposition: form-data;" + "name=\"file\";filename=\""
+ filename + "\"" + END);
//uploadFileOutputStream.writeBytes("Content-Type: application/octet-stream;charset=utf-8" + END);
uploadFileOutputStream.writeBytes(END);
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
LogUtils.i(LOG_TAG, "文件大小 : " + fileInputStream.available());
while (-1 != (length = fileInputStream.read(buffer))) {
uploadFileOutputStream.write(buffer, 0, length);
}
fileInputStream.close();
uploadFileOutputStream.writeBytes(END);
uploadFileOutputStream.writeBytes(PREFIX + BOUNDARY + PREFIX + END);
uploadFileOutputStream.flush();
uploadFileOutputStream.close();
float code = mHttpURLConnection.getResponseCode();
LogUtils.e("response Code : " + code);
if (200 == (int) code) {
InputStream responseStream = mHttpURLConnection.getInputStream();
BufferedReader responseReader = new BufferedReader(new InputStreamReader(responseStream));
StringBuffer responseBuffer = new StringBuffer();
String tmp;
while (null != (tmp = responseReader.readLine())) {
responseBuffer.append(tmp);
}
LogUtils.i(LOG_TAG, responseBuffer.toString());
responseStream.close();
} else {
LogUtils.e("server response error code : " + code);
return null;
}
} catch (MalformedURLException e) {
LogUtils.e(LOG_TAG, e.getMessage());
throw new FeedbackException(e.hashCode(), e.getMessage());
} catch (IOException e) {
LogUtils.e(LOG_TAG, "IOException : " + e.getMessage());
throw new FeedbackException(e.hashCode(), e.getMessage());
} catch (JSONException e) {
LogUtils.e(LOG_TAG, e.getMessage());
throw new FeedbackException(e.hashCode(), e.getMessage());
}
使用jodd库进行上传:
Iterator<String> filenames = file.keys();
jodd.http.HttpRequest req = jodd.http.HttpRequest.post(FeedbackConst.FeedbackUrl.URL_MAIN + FeedbackConst.FeedbackUrl.URL_SUB_UPLOAD)
.accept(PROPERTY_VALUE_ACCEPT)
.cookies(new Cookie(getSessionIdFromSQLite()));
while (filenames.hasNext()) {
String filename = filenames.next();
LogUtils.i("file name : " + filename);
try {
req.form(filename, new File(file.getString(filename)));
} catch (JSONException e) {
e.printStackTrace();
}
}
jodd.http.HttpResponse httpResponse = req.send();
LogUtils.i("url : " + req.url());
LogUtils.i("response code : " + httpResponse.statusCode() + "");
String value = httpResponse.charset("utf-8").bodyText();
LogUtils.i("return length : " + value.length());
value = value.replaceAll("\r|\n", "");
try {
mResponseJSONObject = new JSONObject(value);
} catch (JSONException e) {
throw new FeedbackException(e.hashCode(), e.getMessage());
}