Java EE 目录:https://blog.csdn.net/dkbnull/article/details/87932809
Spring Boot 专栏:https://blog.csdn.net/dkbnull/column/info/26341
Spring Cloud 专栏:https://blog.csdn.net/dkbnull/column/info/36820
IDE:IntelliJ IDEA 2017.1 x64
jdk:1.8.0_91
Spring Boot:2.1.1.RELEASE
import cn.wbnull.springbootdemo.boot.GlobalException;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
@Service
public class LogService {
public void logDownload(String name, HttpServletResponse response) throws Exception {
File file = new File("logs" + File.separator + name);
if (!file.exists()) {
throw new GlobalException(name + "文件不存在");
}
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment;fileName=" + name);
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
} finally {
if (bis != null) {
bis.close();
}
if (fis != null) {
fis.close();
}
}
}
}
import cn.wbnull.springbootdemo.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
@RestController
@Scope("prototype")
@RequestMapping("/log")
public class LogController {
@Autowired
private LogService logService;
/**
* 下载日志接口
*
* @param name
* @param response
* @throws Exception
*/
@GetMapping(value = "/download/{name}")
public void logDownload(@PathVariable String name, HttpServletResponse response) throws Exception {
logService.logDownload(name, response);
}
}
上面接口是要下载logs文件夹下日志文件,以log_20190218.log日志文件为例,浏览器直接访问 http://127.0.0.1:8090/springbootdemo/log/download/log_20190218.log 即可完成下载。
import cn.wbnull.springbootdemo.boot.GlobalException;
import cn.wbnull.springbootdemo.model.ReturnMessage;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@Service
public class LogService {
public JSONObject logUpload(MultipartFile file) throws Exception {
if (file == null || file.isEmpty()) {
throw new GlobalException("未选择需上传的日志文件");
}
String filePath = new File("logs_app").getAbsolutePath();
File fileUpload = new File(filePath);
if (!fileUpload.exists()) {
fileUpload.mkdirs();
}
fileUpload = new File(filePath, file.getOriginalFilename());
if (fileUpload.exists()) {
throw new GlobalException("上传的日志文件已存在");
}
try {
file.transferTo(fileUpload);
return ReturnMessage.success();
} catch (IOException e) {
throw new GlobalException("上传日志文件到服务器失败:" + e.toString());
}
}
}
import cn.wbnull.springbootdemo.service.LogService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
@RestController
@Scope("prototype")
@RequestMapping("/log")
public class LogController {
@Autowired
private LogService logService;
/**
* 上传日志接口
*
* @param file
* @return
* @throws Exception
*/
@PostMapping(value = "/upload")
public JSONObject logUpload(@RequestParam("file") MultipartFile file) throws Exception {
return logService.logUpload(file);
}
}
log-upload.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件上传title>
head>
<body>
<form method="post" action="/log/upload" enctype="multipart/form-data">
<input type="file" name="file">
<br><br>
<input type="submit" value="提交">
form>
body>
html>
浏览器打开页面 http://127.0.0.1:8090/springbootdemo/log-upload.html
点击选择文件打开文件选择页面,选中需要上传的文件后,点击提交,返回如下则为文件上传成功。
我们去logs_app文件夹下查看,生成了上传的日志文件。
上面我们上传文件的客户端为HTML页面,但我们实际生产中未必是WebAPP,这时我们可以通过模拟发送浏览器HTTP请求来实现上传文件。
<dependency>
<groupId>commons-httpclientgroupId>
<artifactId>commons-httpclientartifactId>
<version>3.1version>
dependency>
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
public class LogControllerTests {
@Test
public void logUpload() throws Exception {
String url = "http://127.0.0.1:8090/springbootdemo/log/upload";
String pathname = new File("logs" + File.separator + "log_20190310.log").getCanonicalPath();
logUpload(url, pathname);
}
private static void logUpload(String url, String pathname) {
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
try {
FilePart filePart = new FilePart("file", new File(pathname));
Part[] parts = {filePart};
MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, new HttpMethodParams());
postMethod.setRequestEntity(multipartRequestEntity);
httpClient.executeMethod(postMethod);
String result = postMethod.getResponseBodyAsString();
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
}
}
这里,以下两行代码表示设置多媒体参数,作用类似form表单中的enctype=“multipart/form-data”
MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, new HttpMethodParams());
postMethod.setRequestEntity(multipartRequestEntity);
以下两行代码表示设定参数名称和值,类似form表单中的FilePart filePart = new FilePart("file", new File(pathname)); Part[] parts = {filePart};
public JSONObject logUploads(HttpServletRequest request) throws Exception {
List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
for (MultipartFile file : files) {
logUpload(file);
}
return ReturnMessage.success();
}
/**
* 批量上传日志接口
*
* @param request
* @return
* @throws Exception
*/
@PostMapping(value = "/uploads")
public JSONObject logUploads(HttpServletRequest request) throws Exception {
return logService.logUploads(request);
}
log-uploads.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>批量文件上传title>
head>
<body>
<form method="post" action="log/uploads" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="file" name="file"><br>
<input type="file" name="file"><br>
<br>
<input type="submit" value="提交">
form>
body>
html>
我们先把之前测试上传的logs_app文件夹整个删除,然后浏览器打开页面 http://127.0.0.1:8090/springbootdemo/log-uploads.html
选择要上传的文件
点击提交,返回如下则为上传成功。
我们去logs_app文件夹下查看,生成了上传的日志文件。
这里如果我们也需要Java客户端实现多文件上传,可以仿照2.5实现。
GitHub:https://github.com/dkbnull/SpringBootDemo
微信:https://mp.weixin.qq.com/s/rd6yhfMjwU7Pn3c46bKj7w