MultipartForm_data:没有为公共javax.ws.rs.core.Response类型的参数找到注入源我使用基于泽西岛的RESTfulService实现策略来构建一个用于上传文件的服务。我的服务类名是:UploadFileService.java(参见下面的代码) package com.jerser.service;import java.io.File;import java.io.FileOutputStream;
import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;
import javax.ws.rs.Consumes;import javax.ws.rs.POST;import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;import javax.ws.rs.core.Response;import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;@Path("/fileUpload")public class UploadFileService {
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
String uploadedFileLocation = "d://uploaded/" + fileDetail.getFileName();
// save it
writeToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded to : " + uploadedFileLocation;
return Response.status(200).entity(output).build();
}
// save uploaded file to new location
private void writeToFile(InputStream uploadedInputStream,
String uploadedFileLocation) {
try {
OutputStream out = new FileOutputStream(new File(
uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}}在互联网上,我发现有很多例子展示了如何使用RESTfulAPI上传多部分文件。但有着同样的解决方案。我也不能运行这些代码。我想我把JAR文件搞错了。有人能帮我吗?
3 回答
繁花如伊
TA贡献2012条经验 获得超12个赞
<init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value>org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.moxy.json.MoxyFeature; org.glassfish.jersey.media.multipart.MultiPartFeature</param-value> </init-param>
添加回答
举报
0/150
提交
取消