Jersey API 中的 multipart/form-data 出现 CORS 错误
Posted
技术标签:
【中文标题】Jersey API 中的 multipart/form-data 出现 CORS 错误【英文标题】:CORS Error is coming for multipart/form-data in Jersey API 【发布时间】:2018-08-04 02:35:47 【问题描述】:我正在尝试上传一个 excel 文件,但 Jersey API 中出现了 CORS 错误。我正在使用 multipart/form-data 标头从 angular 4 调用 API,并为每个传入的 JAVA Rest API 请求添加了一个过滤器。找到下面的代码并帮助我:
Angular API 调用:
export class DboperationService
private headers1 = new Headers('Content-Type' : 'multipart/form-data');
sendFile(fileObj: File)
return this._http.post(this.baseURL+'/submitexcel', fileObj,headers : this.headers1).map(res => res.json().data).subscribe();
REST API:
@POST
@Path("/submitexcel")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail)
// check if all form parameters are provided
System.out.println("CAssked");
if (uploadedInputStream == null || fileDetail == null)
return Response.status(400).entity("Invalid form data").build();
// create our destination folder, if it not exists
try
createFolderIfNotExists(UPLOAD_FOLDER);
catch (SecurityException se)
System.out.println("Can not create destination folder on server");
return Response.status(500)
.entity("Can not create destination folder on server")
.header("Access-Control-Allow-Origin", "*")
.build();
String uploadedFileLocation = UPLOAD_FOLDER + fileDetail.getFileName();
/*try
//saveToFile(uploadedInputStream, uploadedFileLocation);
catch (IOException e)
return Response.status(500).entity("Can not save file").build();
*/
System.out.println("File saved to:"+uploadedFileLocation);
return Response.status(200)
.entity("File saved to " + uploadedFileLocation)
.header("Access-Control-Allow-Origin", "*")
.build();
跨域过滤器:
package com.newgen.aproj2;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;
public class CrossOrigin implements ContainerResponseFilter
@Override
public ContainerResponse filter(ContainerRequest creq, ContainerResponse cresp)
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Origin", "*");
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Credentials", "true");
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, HEAD");
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
return cresp;
响应标头:
【问题讨论】:
您确认浏览器确实收到了标头吗?在您喜欢的浏览器网络控制台中查看响应标头。 @Akshay...我更新了浏览器收到的请求/响应标头... 【参考方案1】:我的项目也有同样的东西,我们使用以下标题
"Access-Control-Allow-Origin" : "*"
"Access-Control-Allow-Headers" : "Origin, X-Requested-With, Content-Type, Accept"
也许您只是缺少Access-Control-Allow-Headers
中的Origin
之一
【讨论】:
以上是关于Jersey API 中的 multipart/form-data 出现 CORS 错误的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法在 REST API Jersey 中检测预检请求?
nested exception is com.sun.jersey.api.client.UniformInterfaceException: PUT
使用 ClientResponse Jersey 调用 Rest API