本文主要是介绍工作总结之---上传下载图片文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/**
* @Title: setData
* @Description: 上传文件,将文件信息保存到数据库,前台from提交
* @return: void
*/
@RequestMapping(value = "/saveData", method = RequestMethod.POST)
@ResponseBody
public void saveData(HttpServletRequest request, HttpServletResponse response, TribuneInfoModel tribuneInfoModel) throws IOException{
//保存帖子除文件外其他信息
DefaultMultipartHttpServletRequest multipartHttpServletRequest = (DefaultMultipartHttpServletRequest)request;
String id = UUID.randomUUID().toString().substring(0, 32);
PrintWriter out = null;
try {
List<MultipartFile> multipartFileList = multipartHttpServletRequest.getMultiFileMap().get("file2");//多个文件上传
// MultipartFile multipartFile2 = multipartHttpServletRequest.getFile("file2");//单个文件上传
Properties properties = ConfigUtil.PROPERTIES;
String path = properties.getProperty("imageResourcrsPath");//虚拟路径对应的服务器的真实路径,映射名称为/resource
path = path + "/file";//文件上传真实路径
String relativePath ="/resource/file";
com.tails.system.model.treasure.File file=new com.tails.system.model.treasure.File();//文件信息实体类
for(int i=0; i<multipartFileList.size(); i++){
String fileName2 = multipartFileList.get(i).getOriginalFilename();
if(fileName2 == null || "".equals(fileName2)){
continue;
}
File file2 = new File(fileName2);
File newFile2 = FileUtil.renameFile(file2);//上传文件重命名
String newFileName2 = newFile2.getName();
File targetFile2 = new File(path, newFileName2);
if(!targetFile2.exists()){
targetFile2.mkdirs();
}
multipartFileList.get(i).transferTo(targetFile2);
file.setBusinessId(id);
file.setBusinessType("2");
file.setFileName(fileName2);
file.setFilePath(relativePath+"/"+newFileName2);
fileService.addFile(file);//保存文件信息
}
out = response.getWriter();
out.flush();
out.println("<script>alert('新增成功');window.location.href='/tribuneInfo/tribuneInfoList';</script>");
out.close();
} catch (Exception e) {
e.printStackTrace();
out.flush();
out.println("<script>alert('新增失败');window.location.href='/tribuneInfo/addTirbuneInfo';</script>");
out.close();
}
}
/**
* <p>
* Description: 上传文件重命名
* </p>
*
* @param file
* 文件名
* @return 文件
* @author : gaoying
* @update :
* @date : 2015-7-26
*/
public static File renameFile(File file) {
String body = "";
String ext = "";
Date date = new Date();
int pot = file.getName().lastIndexOf(".");
if (pot != -1) {
// body = date.getTime() + "";
body = UUID.randomUUID().toString().replace("-", "");
ext = file.getName().substring(pot);
} else {
body = (new Date()).getTime() + "";
ext = "";
}
String newName = body + ext;
file = new File(file.getParent(), newName);
return file;
}
/**
* @Title:* @Description: 文件下载
* @return: void
*/
@RequestMapping(value = "/dlFile")
public String downloadTemplateFile(HttpServletRequest request, HttpServletResponse response) {
try{
String fileSavePath = request.getParameter("filePath");//文件上传保存的真实路径
String fileName = request.getParameter("fileName");
File f = new File(fileSavePath);
if(!f.exists()){
f.createNewFile();
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset();
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
OutputStream out=response.getOutputStream();
while ((len = br.read(buf))>0){
out.write(buf,0,len);
}
out.flush();
br.close();
out.close();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
这篇关于工作总结之---上传下载图片文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!