• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

java实现文件上传下载功能

java 搞代码 4年前 (2022-01-05) 139次浏览 已收录 0个评论

这篇文章主要介绍了java实现文件上传下载功能,上传单个或多个文件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现文件上传下载的具体代码,供大家参考,具体内容如下

1.上传单个文件

Controller控制层

 import java.io.File; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;<p style="color:transparent">来源gao!daima.com搞$代!码网</p> import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.support.RedirectAttributes; @Controller @RequestMapping("testup") public class UploadController { private static Logger LG= LoggerFactory.getLogger(UploadController.class); /** * 9.①单个文件上传 * @param file * @param redirectAttributes * @return */ @RequestMapping(value="/upload",method=RequestMethod.POST,consumes="multipart/form-data") public String uploadFile(@RequestParam MultipartFile file,RedirectAttributes redirectAttributes){ if(file.isEmpty()){ redirectAttributes.addFlashAttribute("message", "Plse select file"); return "redirect:/test/index"; } try { String fileName=file.getOriginalFilename(); /*上传文件存储位置*/ String destFileName="D:\\whupload"+File.separator+fileName; File destFile=new File(destFileName); file.transferTo(destFile); //文件上传成功显示 //redirectAttributes.addAttribute("message","upload file success."); redirectAttributes.addFlashAttribute("message", "upload file success."); } catch (Exception e) { //文件上传失败显示 redirectAttributes.addFlashAttribute("message", "upload file fail"); LG.debug(e.getMessage()); } return "redirect:/test/index"; } }

前端页面源码

 <p>上传文件,使用multipart/form-data类型</p> <button type="submit">上传</button>

2.上传多个文件

Controller控制层

 import java.io.File; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.support.RedirectAttributes; @Controller @RequestMapping("testup") public class UploadController { private static Logger LG= LoggerFactory.getLogger(UploadController.class); /** * 9.②多个文件上传 */ @RequestMapping(value="/uploadBatchFile",method=RequestMethod.POST,consumes="multipart/form-data") public String uploadBatchFile(@RequestParam MultipartFile[] files,RedirectAttributes redirectAttributes){ boolean isEmpty=true; try { for (MultipartFile multipartFile : files) { if(multipartFile.isEmpty()){ continue; } String fileName=multipartFile.getOriginalFilename(); String destFileName="D:\\whupload"+File.separator+fileName; File destFile=new File(destFileName); multipartFile.transferTo(destFile); isEmpty=false; } //int i=1/0; //localhost:8086/test/index?message=upload file success //redirectAttributes.addAttribute("message","upload file success."); } catch (Exception e) { // TODO Auto-generated catch block redirectAttributes.addFlashAttribute("message", "upload file fail"); LG.debug(e.getMessage()); return "redirect:/test/index"; } if(isEmpty){ redirectAttributes.addFlashAttribute("message", "Plse select file"); }else{ redirectAttributes.addFlashAttribute("message", "upload file success."); } return "redirect:/test/index"; } }

前端页面源码

  <button type="submit">上传</button>

3.下载文件

Controller控制器

 import java.io.File; import java.net.MalformedURLException; import java.nio.file.Paths; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("testup") public class UploadController { private static Logger LG= LoggerFactory.getLogger(UploadController.class); /** * 10.下载文件 */ @RequestMapping("/download") @ResponseBody public ResponseEntity downloadFile(@RequestParam String fileName){ try { Resource resource=new UrlResource( //拼接下载的文件的原路径 Paths.get("D:/whupload"+File.separator+fileName).toUri()); if(resource.exists() && resource.isReadable()){ return ResponseEntity.ok() .header(HttpHeaders.CONTENT_TYPE, "application/octet-stream") .header(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=\""+ resource.getFilename()+"\"").body(resource); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); LG.debug(e.getMessage()); } return null; } }

前端页面源码

 <p>下载文件,这里设置默认下载文件为Demo.txt,fileName是下载文件名</p>download file

运行效果

最后,需要注意的是,文件上传有默认的大小限制
在配置文件中加入,即可消除限制

 spring.servlet.multipart.max-file-size=-1 spring.servlet.multipart.max-request-size=-1

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是java实现文件上传下载功能的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:java实现文件上传下载功能

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址