gpt4 book ai didi

java - 上传文件, Angular Spring 启动

转载 作者:行者123 更新时间:2023-12-02 08:41:00 25 4
gpt4 key购买 nike

我需要使用 Angular 和 Spring Boot 上传文件。

Angular 服务:

 const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json;multi-part/form-data'})
};

addProduct(productRequest: ProductRequest, creator: string, tradeReference: string, selectedImage: File) {

const trimCreator = creator.trim();
const trimTradeReference = tradeReference.trim();

const formData = new FormData();
formData.append('images', selectedImage);


return this.http.post<any>(baseUrl + '/addProduct', {formData, productRequest, trimCreator, trimTradeReference} , httpOptions)
.pipe(
catchError(this.handleError)
);
}

我的后端 Rest 服务如下:

    @ApiOperation(value = "add product", response = Iterable.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successfully added product") })
@PostMapping("/addProduct")
public void addProduct(@RequestPart(value = "images") MultipartFile[] images,
@RequestPart(value = "product") Product product,
@RequestPart(value = "creator") @ApiParam(value = "creator") String creator,
@RequestPart(value = "tradeReference") @ApiParam(value = "tradeReference") String tradeReference
) throws Exception {

Arrays.asList(images)
.stream()
.forEach(image -> uploadImages(image, product));
traderServices.addProduct(product, creator, tradeReference);
}

错误:

{"timestamp":"2020-04-23T13:35:03.114+0000","status":500,"error":"Internal Server Error","message":"Current request is not a multipart request","trace":"org.springframework.web.multipart.MultipartException: Current request is not a multipart request\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver.resolveArgument(RequestPartMethodArgumentResolver.java:158)\r\n\tat ,"path":"/api/trader/addProduct"}

我尝试像这样更新 PostMapping

@PostMapping(value = "/'addProduct", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE})

我还删除了 Angular 服务中的消耗信息和 header 信息,但它不起作用。

使用 postman ,效果很好。

enter image description here

所以无法理解为什么它不起作用!

谢谢

最佳答案

解决方案:

//Angular 服务:

const options = {} as any;

addProduct(productRequest: ProductRequest, selectedImages: File[]) {

const formData = new FormData();

for (let i = 0; i < selectedImages.length; i++) {
formData.append('images', selectedImages[i]);
}

Object.keys(productRequest).forEach((key) => {formData.append(key, productRequest[key]); });

return this.http.post<any>(baseUrl + '/addProduct', formData , options)
.pipe(
catchError(this.handleError)
);
}

//Spring REST 服务:

 @ApiOperation(value = "add product", response = Iterable.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successfully added product") })
@PostMapping("/addProduct")
public void addProduct(@RequestPart(value = "images") @ApiParam(value = "images") MultipartFile[] images,
@RequestPart(value = "productName") @ApiParam(value = "productName") String productName,
@RequestPart(value = "description") @ApiParam(value = "description") String description,
@RequestPart(value = "realPrice") @ApiParam(value = "realPrice") String realPrice,
@RequestPart(value = "category") @ApiParam(value = "category") String category,
@RequestPart(value = "creator") @ApiParam(value = "creator") String creator,
@RequestPart(value = "tradeReference") @ApiParam(value = "tradeReference") String tradeReference
) throws Exception {

Product p = createProduct(productName, description, Double.valueOf(realPrice), category, creator, tradeReference);
Arrays.asList(images)
.stream()
.forEach(image -> uploadImages(image, p));
traderServices.addProduct(p);
}

请注意,Product 对象被拆分为多个参数,以避免出现“不支持内容类型‘application/octet-stream’”和“不支持内容类型‘multipart/form-data’”等错误。将“realPrice”参数类型更改为 Double,生成“不支持内容类型“application/octet-stream””类型的异常,它适用于字符串。

关于java - 上传文件, Angular Spring 启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61389255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com