将Zip文件发送到Rest WebService

时间:2014-01-14 10:29:10

标签: java spring rest zipinputstream

我正在尝试在我的基于java的应用程序中实现一个方法,该方法涉及将zip文件上传到我的服务器。客户端是java,服务器是java(REST,jboss 7)。在过去,我成功地设法上传图像文件,但现在,我有一个zip文件,我有问题,我的主要疑问是这些问题是客户端相关或服务器相关(或两者)。

所以,我的客户端看起来像这样

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
             (...)

同时在服务器端我有

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

    }

当我尝试使用IOUtils或任何其他阅读器复制文件内容时,会出现问题。我总是得到例外

ZipException too many length or distance symbols

现在,我认为问题可能在于我发送数据的方式,因为文件是zip,但我不确切地知道问题所在。每个人都遇到过类似的问题吗?

0 个答案:

没有答案