springboot上传下载

Posted 一介青衣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot上传下载相关的知识,希望对你有一定的参考价值。

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.huawei.util.Utils;

@RestController
@RequestMapping("/file")
public class UploadController {

public static final String IMAGE_SAVE_PATH = "D://aaaaaaaaaaa";

@GetMapping("/get")
public String get() {
return "我是上传服务";
}

//保存单个图片文件
@PostMapping("upload")
public String uploadFile(@RequestParam("file") MultipartFile multipartFile) {
if (multipartFile.isEmpty() || StringUtils.isBlank(multipartFile.getOriginalFilename())) {
return "上传文件为空";
}
//上传文件类型
String contentType = multipartFile.getContentType();
if (!contentType.contains("")) {
return "文件类型错误";
}
//上传文件名
String fileName = multipartFile.getOriginalFilename();
try {
fileName = saveImg(multipartFile, IMAGE_SAVE_PATH);
} catch (IOException e) {
e.printStackTrace();
}
return fileName;
}

//保存图片
private String saveImg(MultipartFile multipartFile,String path) throws IOException {
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
FileInputStream fileInputStream = (FileInputStream) multipartFile.getInputStream();
String suffixName = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
String fileName = Utils.newUUID() + suffixName;
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path + File.separator + fileName));
byte[] bs = new byte[1024];
int len;
while ((len = fileInputStream.read(bs)) != -1) {
bos.write(bs, 0, len);
}
bos.flush();
bos.close();
return fileName;
}

//第二种上传方式 transferTo() 方法
@PostMapping("uploadByTrans")
public String uploadImage1(@RequestParam("file") MultipartFile multipartFile) {
if (multipartFile.isEmpty() || StringUtils.isBlank(multipartFile.getOriginalFilename())) {
return "上传文件为空";
}
//上传文件类型
String contentType = multipartFile.getContentType();
if (!contentType.contains("")) {
return "文件类型错误";
}
String suffixName = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
String newFileName = IMAGE_SAVE_PATH + File.separator + Utils.newUUID() + suffixName;
File dest = new File(newFileName);
try {
multipartFile.transferTo(dest);
} catch (IllegalStateException e) {
return "上传失败";
} catch (IOException e) {
return "上传失败";
}
return newFileName;
}

//多文件上传
@PostMapping("uploadMult")
public String uploadImage2(HttpServletRequest request) {
List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
String filePath = "D://aaaaaaaaaaa/";
for (int i = 0; i < files.size(); i++) {
MultipartFile file = files.get(i);
if (file.isEmpty()) {
return "上传第" + (i++) + "个文件失败";
}
String fileName = file.getOriginalFilename();
File dest = new File(filePath + fileName);
try {
file.transferTo(dest);
} catch (IOException e) {
return "上传第" + (i++) + "个文件失败";
}
}
return "上传成功";
}

//下载文件
@GetMapping("downLoad")
public void downLoad(HttpServletResponse response) throws UnsupportedEncodingException {
String filename="11.jpg";
String filePath = "D://aaaaaaaaaaa" ;
File file = new File(filePath + "/" + filename);
if(file.exists()){
response.setContentType("application/octet-stream");
response.setHeader("content-type", "application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filename,"utf8"));
byte[] buffer = new byte[1024];
//输出流
OutputStream os = null;
try(FileInputStream fis= new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);) {
os = response.getOutputStream();
int i = bis.read(buffer);
while(i != -1){
os.write(buffer);
i = bis.read(buffer);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

public String newUUID() {
return UUID.randomUUID().toString().replace("-", "").toUpperCase();
}

}

以上是关于springboot上传下载的主要内容,如果未能解决你的问题,请参考以下文章

springboot调整上传文件大小限制

springboot怎么获得文件上传的地址

SpringBoot文件上传

springboot整合vue实现上传下载文件

springboot上传文件写入数据库

SpringBoot.08.SpringBoot文件上传与下载