1、maven 引入依赖
org.apache.httpcomponents
httpmime
4.5.2
org.apache.httpcomponents
httpcore
4.4.9
2、
@RequestMapping("upload")
public ResultMessage upload(HttpServletRequest request,HttpServletResponse response,
@RequestParam(name="userid",required=false)String userid,
@RequestParam(name="bid",required=false)String bid,
@RequestParam(name="pid",required=false)String pid,
@RequestParam(name="reportNumber",required=false)String reportNumber,
@RequestParam(name="plateNumber",required=false)String plateNumber,
@RequestParam("file") MultipartFile file) throws IllegalStateException, IOException{
String filename=file.getOriginalFilename();
String extensionName = filename.substring(filename.lastIndexOf(".") + 1);
String imgPath = System.currentTimeMillis() + "." +extensionName;
String filePath = request.getSession().getServletContext().getRealPath("/")+"uploadimages\\";
File newfile = new File(filePath);
if(!newfile.exists()){
newfile.mkdirs();
}
file.transferTo(new File(filePath+imgPath));
HttpClient client = new DefaultHttpClient();
HttpPost post= new HttpPost("");
MultipartEntity muti = new MultipartEntity();
File newfile1 = new File(filePath+imgPath);
FileBody body = new FileBody(newfile1);
muti.addPart("file",body);
muti.addPart("userid", new StringBody(userid));
muti.addPart("pid", new StringBody(pid));
muti.addPart("bid", new StringBody(bid));
muti.addPart("reportNumber", new StringBody(reportNumber));
muti.addPart("plateNumber", new StringBody(plateNumber));
post.setEntity(muti);
try {
HttpResponse resp = client.execute(post);
if(resp.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
return ResultMessage.getSuccess().setData(EntityUtils.toString(resp.getEntity()));
}
}catch (IOException e) {
logger.info(e.getMessage(),e);
}
return ResultMessage.getFail();
}
org.apache.httpcomponents httpmime 4.5.2