@RequestMapping("/uploader")
public Map<String, Object> uploader(HttpServletRequest request) {
Map<String, Object> map = new HashMap<String, Object>();
try {
if(request.getHeader("content-type")!=null&&"application/x-www-form-urlencoded".equals(request.getHeader("content-type"))){
return null;//不支持断点续传,直接返回null即可
}
//将请求转换成
MultipartHttpServletRequest mRequest=(MultipartHttpServletRequest)request;
System.out.println("mre:"+mRequest);
Enumeration<String> ps = mRequest.getParameterNames();
while(ps.hasMoreElements()){
String hname = ps.nextElement();
System.out.println("hname--"+hname);
System.out.println("mRequest.getParameter(hname)---"+mRequest.getParameter(hname));
}
int eventType=0;
int reportSource=0;
double longitude=0;
double latitude=0;
if(mRequest.getParameter("eventType")!=null&&!"".equals(mRequest.getParameter("eventType").trim()))
eventType= Integer.parseInt(mRequest.getParameter("eventType"));
if(mRequest.getParameter("reportSource")!=null&&!"".equals(mRequest.getParameter("reportSource").trim()))
reportSource= Integer.parseInt(mRequest.getParameter("reportSource"));
String reporter= mRequest.getParameter("reporter");
String phone= mRequest.getParameter("phone");
String title= mRequest.getParameter("title");
String description= mRequest.getParameter("description");
if(mRequest.getParameter("longitude")!=null&&!"".equals(mRequest.getParameter("longitude").trim()))
longitude= Double.parseDouble(mRequest.getParameter("longitude"));
if(mRequest.getParameter("latitude")!=null&&!"".equals(mRequest.getParameter("latitude").trim()))
latitude= Double.parseDouble(mRequest.getParameter("latitude"));
Iterator<String> fns=mRequest.getFileNames();//获取上传的文件列表
System.out.println("fns--"+fns.hasNext());
while(fns.hasNext()){
String s =fns.next();
System.out.println("输出=="+s+"==="+mRequest.getFile(s));
System.out.println(mRequest.getFile(s));//get file success!!!!!
MultipartFile mFile = mRequest.getFile(s);
if(mFile.isEmpty()){
map.put("error", "EventAction.picture.failed");
}else{
// String basePath=Constant.BASEPICUPLOADPATH;
// String dPath= Constant.SDF_PARAM.format(new Date());
File dir = new File("F:\\file\\path");
if(!dir.exists()){
dir.mkdirs();
}
String originFileName=mFile.getOriginalFilename();
String suffix=originFileName.split("\\.")[originFileName.split("\\.").length-1];
String base64Name=UUID.randomUUID().toString();
File file = new File("F:\\file\\path",base64Name+"."+suffix);
try {
FileUtils.copyInputStreamToFile(mFile.getInputStream(),file);//存储文件
} catch (IOException e) {
e.printStackTrace();
}
}
}
//System.out.println(mRequest.getFileNames());
map.put("result", "OK");//返回结果
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
请各位朋友棒棒忙!
0 个回复