package com.example.demo.ctrl;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DownloadServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String downloadFileName = "2.jpg";
ServletContext servletContext = getServletContext();
String mimeType = servletContext.getMimeType("/file/" + downloadFileName);
System.out.println("下载的文件的类型:" + mimeType);
resp.setContentType(mimeType);
resp.setHeader("Content-Disposition","attachment; filename=" + downloadFileName);
InputStream resourceAsStream = servletContext.getResourceAsStream("/file/" + downloadFileName);
OutputStream outputStream = resp.getOutputStream();
IOUtils.copy(resourceAsStream,outputStream);
}
}
package com.example.demo.ctrl;
import com.example.demo.ZipUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
@RestController
@Slf4j
public class FileDownCtrl {
@GetMapping(value = "/downSingleFile")
public String download(HttpServletResponse response, String fileName) throws UnsupportedEncodingException {
String filePathName = fileName;
File file = new File("upload/" + filePathName);
if (!file.exists()) {
return "文件不存在";
}
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filePathName, "utf-8"));
try {
InputStream inStream = new FileInputStream(file);
OutputStream os = response.getOutputStream();
byte[] buff = new byte[1024];
int i = 0;
while ((i = inStream.read(buff)) != -1) {
os.write(buff, 0, i);
}
os.flush();
os.close();
inStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return "下载成功";
}
@GetMapping("/downMultipleFiles")
public String multipleFiles(HttpServletResponse response, String[] fileName) throws IOException {
String downPath = System.getProperty("user.dir") + "/downzip/";
File f = new File(downPath);
if (!f.exists()) {
f.mkdir();
}
for (int i = 0; i < fileName.length; i++) {
String uploadPath = System.getProperty("user.dir") + "/upload/" + fileName[i];
ZipUtils.copyFile(uploadPath, downPath + "/" + fileName[i]);
}
response.setContentType("application/zip");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String format = simpleDateFormat.format(new Date());
String filename = format + "down.zip";
response.setHeader("Content-Disposition", "attachment; filename=" + filename);
ZipUtils.toZip(downPath, response.getOutputStream(), true);
File[] listFiles = f.listFiles();
for (int i = 0; i < listFiles.length; i++) {
listFiles[i].delete();
log.info("正在删除第" + i + "个文件");
}
f.delete();
return "多文件下载成功";
}
}
package com.example.demo.ctrl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
@RestController
@Slf4j
public class FileUploadCtrl {
@PostMapping("/singleFile")
public String singleFile(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return "上传的文件不能为空";
}
try {
log.info("[文件类型ContentType] - [{}]", file.getContentType());
log.info("[文件组件名称Name] - [{}]", file.getName());
log.info("[文件原名称OriginalFileName] - [{}]", file.getOriginalFilename());
log.info("[文件大小] - [{}]", file.getSize());
String path = System.getProperty("user.dir") + "/upload/";
log.info(this.getClass().getName() + "图片路径:" + path);
File f = new File(path);
if (!f.exists()) {
f.mkdir();
}
File dir = new File(path + file.getOriginalFilename());
file.transferTo(dir);
return "上传单个文件成功";
} catch (Exception e) {
e.printStackTrace();
return "上传单个文件失败";
}
}
@PostMapping("/multipleFiles")
public String multipleFiles(@RequestParam("file") MultipartFile[] files) {
if (null == files && files.length == 0) {
return null;
}
String filePath = System.getProperty("user.dir") + "/upload/";
log.info(this.getClass().getName() + "图片路径:" + filePath);
File f = new File(filePath);
if (!f.exists()) {
f.mkdir();
}
for (MultipartFile mf : files) {
String filename = mf.getOriginalFilename();
if (mf.isEmpty()) {
return "文件名称:" + filename + "上传失败,原因是文件为空!";
}
File dir = new File(filePath + filename);
try {
mf.transferTo(dir);
log.info("文件名称:" + filename + "上传成功");
} catch (IOException e) {
log.error(e.toString(), e);
return "文件名称:" + filename + "上传失败";
}
}
return "多文件上传成功";
}
}