@RequestMapping(value = "saveFileupload")
@ResponseBody
public String saveFileupload(HttpServletRequest request,String menutree,HttpServletResponse response){
String fileName="";
String docType="";
//创建一个通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//判断 request 是否有文件上传,即多部分请求
if(multipartResolver.isMultipart(request)){
//转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
//取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
while(iter.hasNext()){
//取得上传文件
MultipartFile file = multiRequest.getFile(iter.next());
if(file != null){
//取得当前上传文件的文件名称
String priNames = file.getOriginalFilename();
String priName=priNames.substring(0,priNames.indexOf("."));
if(!"".equals(priName)){
//重命名上传后的文件名
excelFile = new File(filePath+""+priNames);
docType = priNames.substring((priNames.lastIndexOf(".")));
fileUploadService.savefile(priName, filePath, docType, menutree);
try {
file.transferTo(excelFile);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return JsonMapper.getInstance().toJson("操作成功");
}
/*
* 文件下载
*/
@RequestMapping(value = "downFile")
@ResponseBody
public void downFile(String id,HttpServletRequest request,HttpServletResponse response){
List<Map<String,Object>> list=fileUploadService.downFile(id);
String filetype=String.valueOf(list.get(0).get("filetype"));
try{
//File file = new File(Constant.OPLOAD_PAHT,fileName);
// 读到流中
InputStream inStream = new FileInputStream(list.get(0).get("filecontent")+""+list.get(0).get("filename")+""+""+filetype+"");// 文件的存放路径
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(list.get(0).get("filename")+filetype,"UTF-8"));
// 循环取出流中的数据
byte[] b = new byte[1000];
int len;
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}