有时候我们传的参数很大,比如图片转成的base64字符,如果普通传参肯定会报错,可以把这种参数放在from-data的body里面,我们JAVA后台需要接收from-data的参数。
public ReturnModel fromDateTest(HttpServletRequest request){
ReturnModel returnModel = new ReturnModel();
JSONObject jsonObject = new JSONObject();
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = null;
try {
items = upload.parseRequest(new ServletRequestContext(request));
} catch (FileUploadException e) {
returnModel.setCode(1);
returnModel.setMessage("上传失败");
e.printStackTrace();
}
for(int i=0;i<items.size();i++)
{
String fileName = items.get(i).getFieldName();
String fileValue = items.get(i).getString();
jsonObject.put(fileName, fileValue);
}
returnModel.setCode(0);
returnModel.setMessage("上传成功");
returnModel.setBody("");
return returnModel;
}
正常这样子是可以接收到的。
但是实际情况是这样子的。
很明显,request里面是有值的,但是为什么我们获取不到呢?
后来查看了一些资料才知道使用Apache Commons FileUpload组件上传文件时总是返回null,调试发现ServletFileUpload对象为空,在Spring Boot中有默认的文件上传组件,在使用ServletFileUpload时需要关闭Spring Boot的默认配置 ,禁用MultipartResolverSpring提供的默认值,在配置文件中加入
spring.http.multipart.enabled=false
然后试了一下
就可以获取到了。
下面在贴一下发送from-data请求的代码:
@SuppressWarnings("rawtypes")
public static String formUpload(String urlStr, Map<String, String> textMap) {
String res = "";
HttpURLConnection conn = null;
textMap.put("keyId","111");
textMap.put("photo","222");
// boundary就是request头和上传文件内容的分隔符
String BOUNDARY = "---------------------------123821742118716";
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
// conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
conn.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY);
OutputStream out = new DataOutputStream(conn.getOutputStream());
// text
if (textMap != null) {
StringBuffer strBuf = new StringBuffer();
Iterator iter = textMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String inputName = (String) entry.getKey();
String inputValue = (String) entry.getValue();
if (inputValue == null) {
continue;
}
strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
strBuf.append(inputValue);
}
out.write(strBuf.toString().getBytes());
}
byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
out.write(endData);
out.flush();
out.close();
// 读取返回数据
StringBuffer strBuf = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
strBuf.append(line).append("\n");
}
res = strBuf.toString();
reader.close();
reader = null;
} catch (Exception e) {
System.out.println("发送POST请求出错。" + urlStr);
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
conn = null;
}
}
return res;
}