ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot上传下载

2021-05-28 17:01:19  阅读:151  来源: 互联网

标签:return springboot java 上传下载 file import multipartFile String


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();
}

}

标签:return,springboot,java,上传下载,file,import,multipartFile,String
来源: https://www.cnblogs.com/chengshentao/p/14822944.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有