parent
ca5855c149
commit
d489bdb4ce
@ -1,26 +1,27 @@ |
||||
* [首页](/?t=1557833057337) |
||||
* [首页](/?t=1557902036067) |
||||
* 开发文档 |
||||
* [快速体验](files/10010_快速体验.md?t=1557833057339) |
||||
* [项目接入到SOP](files/10011_项目接入到SOP.md?t=1557833057355) |
||||
* [新增接口](files/10020_新增接口.md?t=1557833057355) |
||||
* [业务参数校验](files/10030_业务参数校验.md?t=1557833057355) |
||||
* [错误处理](files/10040_错误处理.md?t=1557833057355) |
||||
* [编写文档](files/10041_编写文档.md?t=1557833057355) |
||||
* [接口交互详解](files/10050_接口交互详解.md?t=1557833057355) |
||||
* [easyopen支持](files/10070_easyopen支持.md?t=1557833057356) |
||||
* [使用签名校验工具](files/10080_使用签名校验工具.md?t=1557833057356) |
||||
* [ISV管理](files/10085_ISV管理.md?t=1557833057356) |
||||
* [路由授权](files/10090_路由授权.md?t=1557833057356) |
||||
* [接口限流](files/10092_接口限流.md?t=1557833057356) |
||||
* [SDK开发](files/10095_SDK开发.md?t=1557833057356) |
||||
* [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1557833057356) |
||||
* [应用授权](files/10097_应用授权.md?t=1557833057356) |
||||
* [更改数据节点名称](files/10099_更改数据节点名称.md?t=1557833057356) |
||||
* [对接前端](files/10100_对接前端.md?t=1557833057356) |
||||
* [自定义过滤器](files/10102_自定义过滤器.md?t=1557833057356) |
||||
* [快速体验](files/10010_快速体验.md?t=1557902036070) |
||||
* [项目接入到SOP](files/10011_项目接入到SOP.md?t=1557902036086) |
||||
* [新增接口](files/10020_新增接口.md?t=1557902036086) |
||||
* [业务参数校验](files/10030_业务参数校验.md?t=1557902036086) |
||||
* [错误处理](files/10040_错误处理.md?t=1557902036086) |
||||
* [编写文档](files/10041_编写文档.md?t=1557902036086) |
||||
* [接口交互详解](files/10050_接口交互详解.md?t=1557902036087) |
||||
* [easyopen支持](files/10070_easyopen支持.md?t=1557902036087) |
||||
* [使用签名校验工具](files/10080_使用签名校验工具.md?t=1557902036087) |
||||
* [ISV管理](files/10085_ISV管理.md?t=1557902036087) |
||||
* [路由授权](files/10090_路由授权.md?t=1557902036087) |
||||
* [接口限流](files/10092_接口限流.md?t=1557902036087) |
||||
* [SDK开发](files/10095_SDK开发.md?t=1557902036087) |
||||
* [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1557902036087) |
||||
* [应用授权](files/10097_应用授权.md?t=1557902036087) |
||||
* [更改数据节点名称](files/10099_更改数据节点名称.md?t=1557902036087) |
||||
* [对接前端](files/10100_对接前端.md?t=1557902036087) |
||||
* [自定义过滤器](files/10102_自定义过滤器.md?t=1557902036088) |
||||
* [文件上传](files/10104_文件上传.md?t=1557902036088) |
||||
* 原理分析 |
||||
* [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1557833057357) |
||||
* [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1557833057357) |
||||
* [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1557833057357) |
||||
* [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1557833057357) |
||||
* [常见问题](files/90100_常见问题.md?t=1557833057357) |
||||
* [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1557902036088) |
||||
* [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1557902036088) |
||||
* [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1557902036088) |
||||
* [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1557902036088) |
||||
* [常见问题](files/90100_常见问题.md?t=1557902036088) |
||||
|
@ -0,0 +1,109 @@ |
||||
# 文件上传 |
||||
|
||||
请求接口时带上文件 |
||||
|
||||
## 客户端调用 |
||||
|
||||
```java |
||||
DemoFileUploadRequest request = new DemoFileUploadRequest(); |
||||
|
||||
DemoFileUploadModel model = new DemoFileUploadModel(); |
||||
model.setRemark("上传文件参数"); |
||||
request.setBizModel(model); |
||||
|
||||
List<UploadFile> files = new ArrayList<>(); |
||||
String root = System.getProperty("user.dir"); |
||||
System.out.println(root); |
||||
// 这里演示将resources下的两个文件上传到服务器 |
||||
files.add(new UploadFile("file1", new File(root + "/src/main/resources/file1.txt"))); |
||||
files.add(new UploadFile("file2", new File(root + "/src/main/resources/file2.txt"))); |
||||
request.setFiles(files); |
||||
|
||||
DemoFileUploadResponse response = client.execute(request); |
||||
|
||||
System.out.println("--------------------"); |
||||
if (response.isSuccess()) { |
||||
List<DemoFileUploadResponse.FileMeta> responseFiles = response.getFiles(); |
||||
System.out.println("您上传的文件信息:"); |
||||
responseFiles.stream().forEach(file->{ |
||||
System.out.println(file); |
||||
}); |
||||
} else { |
||||
System.out.println("errorCode:" + response.getCode() + ",errorMsg:" + response.getMsg()); |
||||
} |
||||
System.out.println("--------------------"); |
||||
``` |
||||
|
||||
客户端使用`UploadFile`添加上传文件 |
||||
|
||||
```java |
||||
/** |
||||
* @param name 表单名称,不能重复 |
||||
* @param file 文件 |
||||
* @throws IOException |
||||
*/ |
||||
public UploadFile(String name, File file) throws IOException { |
||||
this(name, file.getName(), FileUtil.toBytes(file)); |
||||
} |
||||
``` |
||||
|
||||
其中`name`表示字段名称,相当于`<input type="file" name="file1"/>`中的name |
||||
|
||||
源码详见sop-sdk |
||||
|
||||
## 服务端接收文件 |
||||
|
||||
- 方式1 |
||||
|
||||
```java |
||||
/** |
||||
* 方式1:将文件写在参数中,可直接获取。好处是可以校验是否上传 |
||||
* @param param |
||||
* @return |
||||
*/ |
||||
@ApiMapping(value = "demo.file.upload") |
||||
public FileUploadVO file1(FileUploadParam param) { |
||||
System.out.println(param.getRemark()); |
||||
// 获取上传的文件 |
||||
MultipartFile file1 = param.getFile1(); |
||||
MultipartFile file2 = param.getFile2(); |
||||
... |
||||
return vo; |
||||
} |
||||
|
||||
@Data |
||||
public class FileUploadParam { |
||||
private String remark; |
||||
|
||||
// 上传文件,字段名称对应表单中的name属性值 |
||||
@NotNull(message = "文件1不能为空") |
||||
private MultipartFile file1; |
||||
|
||||
@NotNull(message = "文件2不能为空") |
||||
private MultipartFile file2; |
||||
} |
||||
``` |
||||
|
||||
方式1的好处是可以使用`JSR-303`校验文件是否上传 |
||||
|
||||
- 方式2 |
||||
|
||||
```java |
||||
/** |
||||
* 方式2:从request中获取上传文件 |
||||
* |
||||
* @param param |
||||
* @return |
||||
*/ |
||||
@ApiMapping(value = "demo.file.upload2") |
||||
public FileUploadVO file2(FileUploadParam2 param, HttpServletRequest request) { |
||||
System.out.println(param.getRemark()); |
||||
FileUploadVO vo = new FileUploadVO(); |
||||
// 获取上传的文件 |
||||
Collection<MultipartFile> uploadFiles = UploadUtil.getUploadFiles(request); |
||||
... |
||||
return vo; |
||||
} |
||||
``` |
||||
|
||||
微服务端源码参考`FileUploadDemoController.java` |
@ -0,0 +1,38 @@ |
||||
package com.gitee.sop.servercommon.util; |
||||
|
||||
import org.apache.commons.fileupload.servlet.ServletFileUpload; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
import org.springframework.web.multipart.MultipartHttpServletRequest; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import java.util.Collection; |
||||
import java.util.Collections; |
||||
import java.util.Map; |
||||
import java.util.Optional; |
||||
|
||||
/** |
||||
* 文件上传工具类 |
||||
* |
||||
* @author tanghc |
||||
*/ |
||||
public class UploadUtil { |
||||
|
||||
/** |
||||
* 获取上传文件 |
||||
* |
||||
* @param request |
||||
* @return |
||||
*/ |
||||
public static Collection<MultipartFile> getUploadFiles(HttpServletRequest request) { |
||||
Map<String, MultipartFile> fileMap = null; |
||||
//检查form中是否有enctype="multipart/form-data"
|
||||
if (ServletFileUpload.isMultipartContent(request)) { |
||||
//将request变成多部分request
|
||||
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; |
||||
fileMap = multiRequest.getFileMap(); |
||||
} |
||||
return Optional.ofNullable(fileMap) |
||||
.map(map -> map.values()) |
||||
.orElse(Collections.emptyList()); |
||||
} |
||||
} |
@ -1,56 +1,78 @@ |
||||
package com.gitee.sop.storyweb.controller; |
||||
|
||||
import com.gitee.sop.servercommon.annotation.ApiMapping; |
||||
import com.gitee.sop.servercommon.util.UploadUtil; |
||||
import com.gitee.sop.storyweb.controller.param.FileUploadParam; |
||||
import com.gitee.sop.storyweb.controller.param.FileUploadParam2; |
||||
import com.gitee.sop.storyweb.vo.FileUploadVO; |
||||
import org.apache.commons.fileupload.servlet.ServletFileUpload; |
||||
import org.apache.commons.io.IOUtils; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
import org.springframework.web.multipart.MultipartHttpServletRequest; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import java.io.IOException; |
||||
import java.util.Map; |
||||
import java.util.Collection; |
||||
|
||||
/** |
||||
* 演示文件上传 |
||||
* |
||||
* @author tanghc |
||||
*/ |
||||
@RestController |
||||
public class FileUploadDemoController { |
||||
|
||||
/** |
||||
* 接收客户端上传的文件,然后把文件信息返回给客户端 |
||||
* 方式1:将文件写在参数中,可直接获取。好处是可以校验是否上传 |
||||
* @param param |
||||
* @param request |
||||
* @return |
||||
*/ |
||||
@ApiMapping(value = "demo.file.upload", ignoreValidate = true) |
||||
public FileUploadVO file(FileUploadParam param, HttpServletRequest request) { |
||||
@ApiMapping(value = "demo.file.upload") |
||||
public FileUploadVO file1(FileUploadParam param) { |
||||
System.out.println(param.getRemark()); |
||||
// 获取上传的文件
|
||||
MultipartFile file1 = param.getFile1(); |
||||
MultipartFile file2 = param.getFile2(); |
||||
|
||||
FileUploadVO vo = new FileUploadVO(); |
||||
FileUploadVO.FileMeta fileMeta1 = buildFileMeta(file1); |
||||
FileUploadVO.FileMeta fileMeta2 = buildFileMeta(file2); |
||||
|
||||
vo.getFiles().add(fileMeta1); |
||||
vo.getFiles().add(fileMeta2); |
||||
return vo; |
||||
} |
||||
|
||||
/** |
||||
* 方式2:从request中获取上传文件 |
||||
* |
||||
* @param param |
||||
* @return |
||||
*/ |
||||
@ApiMapping(value = "demo.file.upload2") |
||||
public FileUploadVO file2(FileUploadParam2 param, HttpServletRequest request) { |
||||
System.out.println(param.getRemark()); |
||||
FileUploadVO vo = new FileUploadVO(); |
||||
//检查form中是否有enctype="multipart/form-data"
|
||||
if (ServletFileUpload.isMultipartContent(request)) { |
||||
//将request变成多部分request
|
||||
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; |
||||
Map<String, MultipartFile> fileMap = multiRequest.getFileMap(); |
||||
fileMap.entrySet() |
||||
.stream() |
||||
.forEach(entry->{ |
||||
MultipartFile multipartFile = entry.getValue(); |
||||
try { |
||||
String fileName = multipartFile.getOriginalFilename(); |
||||
long size = multipartFile.getSize(); |
||||
String fileContent = IOUtils.toString(multipartFile.getInputStream(), "UTF-8"); |
||||
FileUploadVO.FileMeta fileMeta = new FileUploadVO.FileMeta(fileName, size, fileContent); |
||||
vo.getFiles().add(fileMeta); |
||||
System.out.println(fileContent); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
}); |
||||
// 获取上传的文件
|
||||
Collection<MultipartFile> uploadFiles = UploadUtil.getUploadFiles(request); |
||||
for (MultipartFile multipartFile : uploadFiles) { |
||||
FileUploadVO.FileMeta fileMeta = buildFileMeta(multipartFile); |
||||
vo.getFiles().add(fileMeta); |
||||
} |
||||
return vo; |
||||
} |
||||
|
||||
private FileUploadVO.FileMeta buildFileMeta(MultipartFile multipartFile) { |
||||
// 文件名
|
||||
String fileName = multipartFile.getOriginalFilename(); |
||||
// 文件大小
|
||||
long size = multipartFile.getSize(); |
||||
// 文件内容
|
||||
String fileContent = null; |
||||
try { |
||||
fileContent = IOUtils.toString(multipartFile.getInputStream(), "UTF-8"); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return new FileUploadVO.FileMeta(fileName, size, fileContent); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,12 @@ |
||||
package com.gitee.sop.storyweb.controller.param; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author tanghc |
||||
*/ |
||||
@Data |
||||
public class FileUploadParam2 { |
||||
private String remark; |
||||
|
||||
} |
@ -0,0 +1 @@ |
||||
file1 content,内容1 |
@ -0,0 +1 @@ |
||||
file2 content,内容2 |
@ -1 +1 @@ |
||||
file1 content |
||||
file1 content,内容1 |
@ -1 +1 @@ |
||||
file2 content... |
||||
file2 content,内容2 |
Loading…
Reference in new issue