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,上传,下载
0
投稿
猜你喜欢
Android 利用反射+try catch实现sdk按需引入依赖库的方法
2022-09-19 22:09:08
Java高版本Api在Android中的使用方法详解
2023-01-19 20:03:08
Java Set集合及其子类HashSet与LinkedHashSet详解
2023-11-26 11:39:35
C# 多线程中经常访问同一资源可能造成哪些问题
2022-09-08 05:22:47
Android Studio下载与安装简易教程
2022-05-28 10:25:30
Android中CountDownTimer 实现倒计时功能
2022-01-03 00:00:24
Docker搭建前端Java的开发环境详解
2023-05-21 04:23:25
java 中锁的性能提高办法
2021-07-28 10:50:28
spring boot mybatis枚举映射示例代码
2023-03-01 11:45:51
最简单易懂的java数组排序方法整理
2023-01-03 18:56:41
springboot手写一个自己的starter源码
2021-07-31 10:18:14
Java+Swing实现五子棋游戏的示例代码
2022-06-27 11:56:22
Java 实战项目锤炼之网上图书馆管理系统的实现流程
2021-10-09 02:05:34
Java实现InputStream的任意拷贝方式
2022-04-08 10:11:32
详解Mybatis注解写法(附10余个常用例子)
2023-01-19 03:39:53
Java实现中英文词典功能
2021-06-20 18:25:56
Java实现断点续传功能的示例代码
2021-12-27 10:07:56
C# API中模型与它们的接口设计详解
2023-11-03 19:49:01
C#并发编程入门教程之概述
2022-08-08 09:44:23
MyBatis-Plus框架整合详细方法
2022-12-14 12:00:46