1,文件上传
@ResponseBody
@RequestMapping ( "/fileUpload" )
public String fileUpload2( @RequestParam ( "file" ) MultipartFile file) throws IOException {
String filename=new Date().getTime() + file.getOriginalFilename();
//获取配置文件定义的路径
String path = SystemProperties.getProperty(SystemProperties.APP_FILE_PATH) + filename;
File newFile = new File(path);
file.transferTo(newFile);
return Success(filename);
}
2,文件下载
/**
//文件返回
*/
@ResponseBody
@RequestMapping(value="/getBannerFileById")
public void getBannerFileById(@RequestParam(value = "id", required = false) String id, HttpServletRequest request, HttpServletResponse response) {
List<Map<String, Object>> list = iBannerService.getBannerById(id);
String path = SystemProperties.getProperty(SystemProperties.APP_FILE_PATH) + list.get(0).get("filename");
File file = new File(path);
// 后缀名
//判断文件是否存在如果不存在就返回默认图标
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
int length = inputStream.read(data);
inputStream.close();
response.setContentType("image/png;charset=utf-8");
OutputStream stream = response.getOutputStream();
stream.write(data);
stream.flush();
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}