一。文件上传
package com.test.web;
import org.springframework.web.bind.annotation.GetMapping;
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 java.io.File;
import java.io.IOException;
/**
* @Author lhc
* @Date 2021/10/22 14:06
*/
@RestController
@RequestMapping("/file")
public class FileController {
@GetMapping("/upload")
public File upload(@RequestParam(value = "file") MultipartFile multipartFile) {
String fileDir = "D:\\substation\\upload\\";
String fileName = multipartFile.getOriginalFilename();
File newFile = new File(fileDir + fileName);
//校验文件夹目录是否存在,不存在就创建一个目录
if (!newFile.getParentFile().exists()) {
boolean mkdirsResult = newFile.getParentFile().mkdirs();
if (!mkdirsResult) {
System.out.println("创建目录:" + newFile.getParentFile() + "失败");
return null;
}
}
try {
//创建文件
boolean newFile1 = newFile.createNewFile();
//填充文件内容
multipartFile.transferTo(newFile);
} catch (IOException e) {
e.printStackTrace();
}
return newFile;
}
}
二。文件下载
选择地址下载参数要有HttpServletResponse response
默认直接下载参数不要有HttpServletResponse response
1。客户端选择地址下载
package com.test.web;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@RestController
@RequestMapping("/file")
public class FileController1 {
/**
* 文件下载(客户端选择下载地址)
*
* @param response response
*/
@GetMapping("/download")
public void download(HttpServletResponse response) {
//服务器文件路径
String filePath = "D:\\substation\\";
//服务器文件名
String fileName = "智能站出图0802.zip";
//服务器文件
File file = new File(filePath, fileName);
//校验服务器文件是否存在
if (!file.exists()) {
System.out.println("找不到服务器指定文件");
return;
}
response.setCharacterEncoding("utf-8");
//防止文件中文名乱码
downLoadFileName = URLEncoder.encode(downLoadFileName, "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + downLoadFileName + ".xlsx");
byte[] buffer = new byte[1024];
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
OutputStream outputStream = null;
try {
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
outputStream = response.getOutputStream();
int i = bufferedInputStream.read(buffer);
while (i != -1) {
outputStream.write(buffer, 0, i);
i = bufferedInputStream.read(buffer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2。直接下载
package com.test.web;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
@RestController
@RequestMapping("/file")
public class FileController2 {
/**
* 文件下载(直接下载)
*/
@GetMapping("/downloadDirect")
public void downloadDirect() {
//服务器文件路径
String filePath = "D:\\substation\\";
//服务器文件名
String fileName = "智能站出图0802.zip";
//服务器文件
File file = new File(filePath, fileName);
//本地文件路径
String newFilePath = "C:\\substation\\";
//本地文件名
String newFileName = "智能站出图0802新.zip";
//本地文件
File newFile = new File(newFilePath, newFileName);
//校验服务器文件是否存在
if (!file.exists()) {
System.out.println("找不到服务器指定文件");
return;
}
// 校验本地文件夹目录是否存在,不存在就创建一个目录
if (!newFile.getParentFile().exists()) {
boolean mkdirsResult = newFile.getParentFile().mkdirs();
if (!mkdirsResult) {
System.out.println("创建目录:" + newFile.getParentFile() + "失败");
return;
}
}
byte[] buffer = new byte[1024];
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
OutputStream outputStream = null;
try {
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
outputStream = new FileOutputStream(newFile);
int i = bufferedInputStream.read(buffer);
while (i != -1) {
outputStream.write(buffer, 0, i);
i = bufferedInputStream.read(buffer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}