1

I am trying to set up file upload example using JAX RS. I could set up the project and successfully upload file in a server location. But i get the following error when file size is more than 10KB (weird!!)

com.sun.jersey.api.client.UniformInterfaceException: POST http://localhost:9090/DOAFileUploader/rest/file/upload returned a response status of 400
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:607)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:507)
at com.sony.doa.rest.client.DOAClient.upload(DOAClient.java:75)
at com.sony.doa.rest.client.DOAMain.main(DOAMain.java:34)

I am new to JAX RS and i'm not sure what exactly the issue is. Do i need to set some parameters client side or server side (like size, timeout etc)?

This is the client side code calling webservice:

public void upload() {
    File file = new File(inputFilePath);
    FormDataMultiPart part = new FormDataMultiPart();
    part.bodyPart(new FileDataBodyPart("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE));
    WebResource resource = Client.create().resource(url);
    String response = resource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, part);
    System.out.println(response);
}

This is the server side code:

@Path("/file")
public class UploadFileService {

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {

    String uploadedFileLocation = "e://uploaded/"
            + fileDetail.getFileName();

    writeToFile(uploadedInputStream, uploadedFileLocation);

    String output = "File uploaded to : " + uploadedFileLocation;

    return Response.status(200).entity(output).build();

}

private void writeToFile(InputStream uploadedInputStream,
        String uploadedFileLocation) {

    try {
        OutputStream out = new FileOutputStream(new File(
                uploadedFileLocation));
        int read = 0;
        byte[] bytes = new byte[16000];

        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();
    } } } 

Please let me know what settings i have to change for file sizes greater than 10KB?

Thanks!

4

2 回答 2

0

我在泽西岛遇到了同样的问题。我已经激活了球衣跟踪,但没有任何帮助。

我已经通过 apache Library 更改了库,并且我看到了链接到存储库以获取 tomcat 临时文件的问题。存储库不存在。对于 10k 以下的文件,未使用存储库。

因此,在创建存储库之后,我使用了 jersey 库并且一切正常。

于 2012-05-23T12:04:04.633 回答
0

我在 Jersey 上下文中使用 org.apache.commons.fileupload.servlet.ServletFileUpload,它工作正常。是的,它设置了最大文件大小,抱歉我之前错过了这个。

这是我使用的代码片段(这是一个多部分表单,因此文件中还有其他字段)

private LibraryUpload parseLibraryUpload(HttpServletRequest request) {
LibraryUpload libraryUpload;

File libraryZip = null;
String name = null;
String version = null;

ServletFileUpload upload = new ServletFileUpload();
upload.setFileSizeMax(MAX_FILE_SIZE);

FileItemIterator iter;
try {
  iter = upload.getItemIterator(request);
while (iter.hasNext()) {
....
if (item.isFormField()) {
....
}else{
BufferedInputStream buffer = new BufferedInputStream(stream);
      buffer.mark(MAX_FILE_SIZE);
      libraryZip = File.createTempFile("fromUpload", null);
      IOUtils.copy(buffer, new FileOutputStream(libraryZip));
...
}
于 2012-04-23T16:27:04.427 回答