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;
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= Logger(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 OriginalFilename();
/*上传⽂件存储位置*/
String destFileName="D:\\whupload"+File.separator+fileName;
File destFile=new File(destFileName);
/
/⽂件上传成功显⽰
//redirectAttributes.addAttribute("message","upload file success.");
redirectAttributes.addFlashAttribute("message", "upload file success.");
} catch (Exception e) {
//⽂件上传失败显⽰
redirectAttributes.addFlashAttribute("message", "upload file fail");
LG.Message());
}
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= Logger(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 OriginalFilename();
String destFileName="D:\\whupload"+File.separator+fileName;
File destFile=new File(destFileName);
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.Message());
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.MalformedURLException;
import java.nio.file.Paths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.Resource;
import 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= Logger(UploadController.class);
/**
* 10.下载⽂件
*/inputtypefile不上传文件
@RequestMapping("/download")
@ResponseBody
public ResponseEntity<Resource> downloadFile(@RequestParam String fileName){
try {
Resource resource=new UrlResource(
/
/拼接下载的⽂件的原路径
<("D:/whupload"+File.separator+fileName).toUri());
ists() && resource.isReadable()){
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_TYPE, "application/octet-stream")
.header(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=\""+
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
LG.Message());
}
return null;
}
}
前端页⾯源码
<p>下载⽂件,这⾥设置默认下载⽂件为,fileName是下载⽂件名</p>
<a href="/testup/download?" rel="external nofollow" >download file</a>
运⾏效果
最后,需要注意的是,⽂件上传有默认的⼤⼩限制
在配置⽂件中加⼊,即可消除限制
spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。