0

I'm trying to implement a method in my java based application that involves uploading a zip file to my server. Client is java, server is java (REST , jboss 7) . In the past I successfully managed to upload image files, but now, with a zip file i am having issues and my main doubt is if these issues are client related or server related (or both) .

So , my client looks like this

final HttpHeaders headers = HttpClientUtils.headersJSONAndAcceptJSON();

    MultiValueMap<String, Object> requestMap = new LinkedMultiValueMap<String, Object>();

    addMap("filename", filename, requestMap);
    addMap("contenttype", contentType, requestMap);
    addMap("type", type, requestMap);

    try {

    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    int b = -1;
            //file data is the inputstream created from the File
    while ( (b = filedata.read())>= 0 ) {
        bout.write(b);
    }

    ByteArrayResource rs = new ByteArrayResource( bout.toByteArray() ){
            @Override
            public String getFilename() {
            return "";
             }
    };
    addMap("resource", rs, requestMap);
    } catch (IOException e1) {
        throw new IllegalStateException("Error");
    }


    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    headers.setAccept(Arrays.asList(HttpClientUtils.mtypeJSONUtf8()));


    final String url = this.baseURL + summaryURL;

    try {
        ResponseEntity<Summary> rEntity = restTemplate.exchange(
                url, 
                HttpMethod.POST,
                HttpClientUtils.entity(headers, requestMap), 
                Summary.class
             (...)

and meanwhile on the server side I have

@POST
@Path("/")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("application/json; charset=UTF-8")
public Summary addImportedSummary(@MultipartForm FileUploadFormObj imp)
{
    Summary importedSummary = new Summary();
    Map<String , String> newpath = new HashMap<String, String>();

    if(imp.getFileData() != null)
    {
        ZipInputStream zip = new ZipInputStream(imp.getFileData());

        ZipEntry entry;

        try {
            while ((entry = zip.getNextEntry()) != null)
            {
                if(entry.getName().endsWith(".html") || entry.getName().endsWith(".htm"))
                {
                    if(entry.getSize() > 0)
                    {

                        StringWriter writer = new StringWriter();

                        IOUtils.copy(zip, writer, "UTF-8");
                        String content = writer.toString();
                        //do something with the content

                    }
                }
            }
            zip.close();
        } catch (IOException e) {

            throw new BadRequestException("Error " + e);
        }

    }

The problem happens when I try to copy the file content with IOUtils or any other reader. I always get the exception

ZipException too many length or distance symbols

Now, I think the problem might be in the way I am sending the data due to the file being a zip but I don't know exactly where the problem is. Did everyone ever ran into a similar problem?

NokusFerreira
  • 135
  • 3
  • 12
  • Try to unzip the file in a standalone program. This has nothing to do with REST or Spring. –  Jan 14 '14 at 10:38
  • 2
    Did you check that the original file is a valid ZIP archive? Also, consider using `IOUtils.toByteArray(filedata)` from [commons io](http://commons.apache.org/proper/commons-io/) instead of rolling your own (incredibly inefficent) file-read loop. – Aaron Digulla Jan 14 '14 at 10:40
  • @AaronDigulla thank you for your answer. True, the loop was very inefficient and also quite old. And that was the problem. I changed it and used what you recomended . I ended up with a much simpler client `byte [] data =IOUtils.toByteArray(filedata); addMap("resource", data, requestMap); } catch (IOException e1) { throw new IllegalStateException("Erro"); } headers.setContentType(MediaType.MULTIPART_FORM_DATA); headers.setAccept(Arrays.asList(HttpClientUtils.mtypeJSONUtf8())); (...)` – NokusFerreira Jan 14 '14 at 10:53
  • I'm a bit wary about your comment; the loop is inefficient but it should be correct. It's possible there is a bug elsewhere. – Aaron Digulla Jan 14 '14 at 12:29

0 Answers0