gpt4 book ai didi

java - 文件正在使用 Content-Disposition header 保存

转载 作者:行者123 更新时间:2023-11-30 06:31:18 26 4
gpt4 key购买 nike

我有 angular2 前端和 Dropwizard 后端。我正在尝试从前端上传图片到后端。

我的html代码:

<input type="file" name="file" (change)="fileChange($event)">

我的组件:

fileChange(event) {
let fileList: FileList = event.target.files;
if (fileList.length > 0) {
let file: File = fileList[0];
let formData:FormData = new FormData();
formData.append('file', file);
this.siteDescriptionService.sendPhoto(formData).subscribe(value => {
console.log("value", value);
});
}
}

我的服务:

sendPhoto(data): Observable<any> {
return this.http.postPhoto('api/site/savePhoto', data, null).map(res => res);
}

我的http拦截器:

postPhoto(url: string, params?: any, options?: RequestOptionsArgs): Observable<any> {
this.beforeRequest();
let headers = new Headers();
headers.append('Content-Type', 'multipart/form-data');
let reqOptions = new RequestOptions({ headers: headers });
return super.post(this.getFullUrl(url), params, reqOptions)
.catch(this.onCatch)
.do((res: Response) => {
this.onSuccess(res);
}, (error: any) => {
this.onError(error);
})
.finally(() => {
this.onFinally();
});
}

请求正在使用这样的有效负载发送:

------WebKitFormBoundaryAz4AnN4lFPWKUvmH内容处置:表单数据;名称="file";文件名=“标志.png”内容类型:图像/png

------WebKitFormBoundaryAz4AnN4lFPWKUvmH--

在我的服务器上我有:

@POST
@Timed
@Path("savePhoto")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadFile(InputStream uploadedInputStream) throws IOException {
String uploadedFileLocation = "/tmp/photo1.png";


FormDataMultiPart part = new FormDataMultiPart().field("file", uploadedInputStream, MediaType.TEXT_PLAIN_TYPE);

FormDataBodyPart p = part.getField("file");
InputStream i = (InputStream) p.getEntity();

writeToFile( i, uploadedFileLocation);

String output = "File uploaded to : " + uploadedFileLocation;
return Response.ok(output).build();
}

private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation)
throws IOException {
int read;
final int BUFFER_LENGTH = 1024;
final byte[] buffer = new byte[BUFFER_LENGTH];
OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
out.flush();
out.close();
}

一切都很好,文件正在保存,但它是与整个请求负载一起保存的,包括 Content-DispositionContent-Type header 等,因此文件变得“损坏”。

如何从文件中删除 Content-Disposition header ?

最佳答案

当您使用InputStream参数时,您是在说您想要整个请求正文。如果您只想要单个部件,则需要使用 @FormDataParam 以及部件名称对其进行注释

public Response uploadFile(@FormDataParam("file") InputStream file,
@FormDataParam("file") FormDataContentDisposition fdcd) {
String filename = fcdc.getFileName();

// You don't need to create the FormDataMultiPart
// just save the InputStream parameter
}

为了使其正常工作,您还需要注册MutliPartFeature

env.jersey().register(MultiPartFeature.class);

关于java - 文件正在使用 Content-Disposition header 保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46051136/

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