mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
修复图片上传保存后图片破损问题
This commit is contained in:
@@ -80,15 +80,16 @@ public class IndexFilter implements WebFilter {
|
||||
if (request.getMethod() == HttpMethod.POST) {
|
||||
ServerRequest serverRequest = ServerWebExchangeUtil.createReadBodyRequest(exchange);
|
||||
// 读取请求体中的内容
|
||||
Mono<?> modifiedBody = serverRequest.bodyToMono(String.class)
|
||||
.flatMap(body -> {
|
||||
Mono<?> modifiedBody = serverRequest.bodyToMono(byte[].class)
|
||||
.flatMap(data -> {
|
||||
String body = new String(data, SopConstants.CHARSET_UTF8);
|
||||
// 构建ApiParam
|
||||
ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange, body);
|
||||
// 签名验证
|
||||
doValidate(exchange, apiParam);
|
||||
return Mono.just(body);
|
||||
return Mono.just(data);
|
||||
});
|
||||
BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody, (Class) String.class);
|
||||
BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody, (Class)byte[].class);
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.putAll(exchange.getRequest().getHeaders());
|
||||
|
||||
|
@@ -14,8 +14,10 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 演示文件上传
|
||||
@@ -70,6 +72,25 @@ public class FileUploadDemoController {
|
||||
return result;
|
||||
}
|
||||
|
||||
@ApiMapping(value = "demo.file.upload3")
|
||||
public FileUploadResult file3(FileUploadParam2 param, HttpServletRequest request) {
|
||||
System.out.println(param.getRemark());
|
||||
FileUploadResult result = new FileUploadResult();
|
||||
// 获取上传的文件
|
||||
Collection<MultipartFile> uploadFiles = UploadUtil.getUploadFiles(request);
|
||||
Optional<MultipartFile> first = uploadFiles.stream().findFirst();
|
||||
if (first.isPresent()) {
|
||||
MultipartFile multipartFile = first.get();
|
||||
try {
|
||||
String path = System.getProperty("user.dir");
|
||||
multipartFile.transferTo(new File(path + "/img_"+System.currentTimeMillis()+".png"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private FileUploadResult.FileMeta buildFileMeta(MultipartFile multipartFile) {
|
||||
// 文件名
|
||||
String fileName = multipartFile.getOriginalFilename();
|
||||
|
BIN
sop-test/src/main/resources/img.png
Normal file
BIN
sop-test/src/main/resources/img.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
@@ -239,6 +239,29 @@ public class AllInOneTest extends TestBase {
|
||||
client.execute(requestBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 演示文件上传
|
||||
*/
|
||||
public void testFile3() {
|
||||
Client client = new Client(url, appId, privateKey);
|
||||
String root = System.getProperty("user.dir");
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("demo.file.upload3")
|
||||
.version("1.0")
|
||||
.bizContent(new BizContent().add("remark", "test file upload"))
|
||||
// 添加文件
|
||||
.addFile("image", new File(root + "/src/main/resources/img.png"))
|
||||
.callback((requestInfo, responseData) -> {
|
||||
System.out.println(responseData);
|
||||
JSONObject jsonObject = JSON.parseObject(responseData);
|
||||
JSONObject data = jsonObject.getJSONObject(requestInfo.getDataNode());
|
||||
Assert.assertEquals(data.getString("code"), "10000");
|
||||
})
|
||||
;
|
||||
|
||||
client.execute(requestBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载文件
|
||||
*/
|
||||
|
Reference in New Issue
Block a user