MultipartFile 上传文件错误 handleHttpRequestMethodNotSupport
/用上传文件的类 MultipartFile 处理上传请求,发现总是报错: Spring MVC-Request method 'POST' not supported: org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
去掉相应的MultipartFile则错误消失,debug源码,分别试了好多修改方法:
从request里去获取MultipartFile对象。
public String uploadPics(@RequestParam(value = "type", required = true) String type,HttpServletRequest request) {
....
// 检测是否为上传请求
String contentType = request.getContentType();
if (contentType != null && contentType.toLowerCase().startsWith("multipart/")) {
MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
MultipartFile file = multipartRequest.getFile("file");
....
}
....
}
2.加拦截器,说是需要隐藏post方法 org.springframework.web.filter.HiddenHttpMethodFilter
发现还是不行,最后追本溯源,发现缺少相应的拦截器,就是处理MultipartFile的文件的时候,需要单独配置一个拦截器:
<beans:bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为1MB -->
<beans:property name="maxUploadSize" value="10000000">
</beans:property>
</beans:bean>
这个拦截器也是
package org.springframework.web.multipart.commons;
加入的功能,加入之后,以上错误消失。
public class FileUploadUtil {
private static Logger logger = LoggerFactory.getLogger(FileUploadUtil.class);
public static String uploadUrl = "https://elongcdn.com/***";
public static String testUploadUrl = "http://1*3:8080/i/upload";
public static HttpClient httpclient = HttpClients.createDefault();
public static String upload(String path) {
try {
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder
.addBinaryBody("file", new File(path))
.setMode(HttpMultipartMode.RFC6532);
HttpPost httpPost = new HttpPost(uploadUrl);
httpPost.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 600000);
httpPost.setEntity(multipartEntityBuilder.build());
HttpResponse httpResponse = null;
httpResponse = httpclient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String content = EntityUtils.toString(httpEntity);
logger.info(content);
return content;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
}
期间有未指定host错误,原因是上传接口需要加http://,否则找不到
学着温习了下httpclient
package com.elong.web.flight.service.utils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
/**
* 往inf-img图片中心上传文件的专用类
*/
public class FileUploadUtil {
private static Logger logger = LoggerFactory.getLogger(FileUploadUtil.class);
public static final String url = "http://192.168.15.43:8080/i/upload";
public static HttpClient httpClient = new DefaultHttpClient();
public static String upload(String path) {
File upload = new File(path);
HttpPost httpPost = new HttpPost(url);
try {
FileBody bin = new FileBody(new File(path));
HttpEntity reqEntity = MultipartEntityBuilder.create()
// 相当于<input type="file" name="file"/>
.addPart("file", bin)
.build();
httpPost.setEntity(reqEntity);
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
HttpResponse httpResponse = httpClient.execute(httpPost);
logger.info("上传成功 " + path);
InputStream is = null;
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
is = entity.getContent();
}
StringBuffer sbf = new StringBuffer();
if (is != null) {
BufferedReader bf = new BufferedReader(new InputStreamReader(is, "utf-8"));
String line;
while ((line = bf.readLine()) != null) {
sbf.append(line);
}
}
String data = sbf.toString();
return data;
} catch (IOException e) {
return "";
}
}
}
package com.elong.web.flight.service.utils;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
/**
* Created by user on 17/12/11.
*/
public class FileUploadUtil {
private static Logger logger = LoggerFactory.getLogger(FileUploadUtil.class);
public static String uploadUrl = "http://pavo.elongstatic.com/i/upload";
public static String accessUrl = "http://pavo.elongstatic.com";
public static String testUploadUrl = "http://192.168.15.43:8080/i/upload";
public static HttpClient httpclient = HttpClients.createDefault();
public static String upload(String path) {
try {
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder
.addBinaryBody("file", new File(path))
.setMode(HttpMultipartMode.RFC6532);
HttpPost httpPost = new HttpPost(uploadUrl);
httpPost.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 600000);
httpPost.setEntity(multipartEntityBuilder.build());
HttpResponse httpResponse = null;
httpResponse = httpclient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String content = EntityUtils.toString(httpEntity);
logger.info(content);
return content;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
}