java实现文件上传下载功能
作者:wh456413 时间:2021-11-26 17:19:05
本文实例为大家分享了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;
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>
<form action="/testup/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">上传</button>
</form>
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";
}
}
前端页面源码
<form action="/testup/uploadBatchFile" method="post" enctype="multipart/form-data">
<input type="file" name="files">
<input type="file" name="files">
<button type="submit">上传</button>
</form>
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<Resource> 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>
<a href="/testup/download?fileName=Demo.txt" rel="external nofollow" >download file</a>
运行效果
最后,需要注意的是,文件上传有默认的大小限制
在配置文件中加入,即可消除限制
spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1
来源:https://blog.csdn.net/wh456413/article/details/107302180
标签:java,上传,下载
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中CyclicBarrier 循环屏障
2021-11-24 08:22:24
![](https://img.aspxhome.com/file/2023/3/76223_0s.png)
Java可变个数形参的方法实例代码
2023-01-15 18:35:56
![](https://img.aspxhome.com/file/2023/2/62372_0s.png)
java Long类型转为String类型的两种方式及区别说明
2022-11-12 18:03:38
Spring @CrossOrigin 注解原理实现
2022-09-03 10:40:04
![](https://img.aspxhome.com/file/2023/1/71851_0s.png)
JVM Client和Server端有什么区别
2023-08-05 22:49:53
![](https://img.aspxhome.com/file/2023/5/64385_0s.png)
android打开本地图像的方法
2022-10-26 08:01:42
C#中的延时函数sleep
2022-08-05 13:16:49
3种C# 加载Word的方法
2021-06-05 21:06:41
![](https://img.aspxhome.com/file/2023/2/84482_0s.png)
C#中float的取值范围和精度分析
2023-05-19 01:03:21
![](https://img.aspxhome.com/file/2023/1/85731_0s.png)
详解java Collections.sort的两种用法
2023-11-28 09:30:31
![](https://img.aspxhome.com/file/2023/3/60473_0s.png)
Java IO之File 类详解
2023-08-07 20:02:12
![](https://img.aspxhome.com/file/2023/2/57802_0s.png)
SpringBoot集成ElasticSearch的示例代码
2023-05-02 05:50:28
Kafka Producer中的消息缓存模型图解详解
2022-05-03 06:00:13
![](https://img.aspxhome.com/file/2023/2/61012_0s.jpg)
springmvc+shiro自定义过滤器的实现代码
2021-08-11 21:23:11
![](https://img.aspxhome.com/file/2023/9/73059_0s.png)
替换so文件来动态替换Flutter代码实现详解
2023-06-23 16:24:06
![](https://img.aspxhome.com/file/2023/9/83549_0s.png)
浅入浅出的讲解Spring循环依赖问题
2023-11-03 07:16:11
![](https://img.aspxhome.com/file/2023/5/79285_0s.png)
SpringBoot中利用AOP和拦截器实现自定义注解
2022-09-14 00:26:53
![](https://img.aspxhome.com/file/2023/1/72391_0s.jpg)
Java类的初始化顺序知识点总结
2021-09-23 23:19:26
![](https://img.aspxhome.com/file/2023/8/63748_0s.png)
使用idea解决maven依赖冲突的问题
2021-10-16 12:32:51
![](https://img.aspxhome.com/file/2023/0/71160_0s.png)
Java实现PDF转为Word文档的示例代码
2021-10-05 02:19:26
![](https://img.aspxhome.com/file/2023/7/62657_0s.jpg)