InputStream无限循环

时间:2013-07-25 14:36:38

标签: java tomcat file-upload jersey

我正在尝试将文件从Android上传到运行Jersey的Tomcat服务器。我将它打包在一个Post Request中。

这就是我在Android中所做的:

protected String doInBackground(String... params) {
    String url = params[0];
    String pathToFile = params[1];
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpPost httpPost = new HttpPost();
    HttpResponse response = null;
    httpPost.addHeader("Cookie", "sessionToken=~session");
    try {
        httpPost.setURI(new URI(url));
        httpPost.setHeader("Accept", "application/json");
        MultipartEntity entity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);
        File file = new File(pathToFile);
        if(!file.exists())
            return null;
        FileBody fileBody = new FileBody(file);
        entity.addPart("file", fileBody);
        httpPost.setEntity(entity);

        response = httpClient.execute(httpPost);
        HttpEntity result = response.getEntity();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch(Exception e){
        e.printStackTrace();
    }
    return response.getStatusLine().toString();

在服务器中,我有以下内容:

我收到一个类型为“org.jvnet.mimepull.DataHead $ ReadMultiStream”的InputStream,在读取它时,读取到达文件末尾后的1024.

@POST
@Consumes({ MediaType.MULTIPART_FORM_DATA })
@Produces({ MediaType.APPLICATION_JSON })
public Response uploadStorageFile(@Context UriInfo ui, @Context HttpHeaders hh, @FormDataParam("file") 
InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail){
    System.out.println(uploadedInputStream.getClass().getName());
    String uploadedFileLocation = fileDetail.getFileName();
    long size = fileDetail.getSize();
    // save it
    writeToFile(uploadedInputStream);

    String output = "File uploaded to : ";

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

}
private void writeToFile(InputStream uploadedInputStream) {

        try {
            OutputStream out = new FileOutputStream(new File("test.jpg"));
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = uploadedInputStream.read(bytes)) > 0) {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

如果文件的长度是8192,则循环如下: 1024,2048,3072,4096,5120,6144,7168,8192,1024 - >为什么呢?

注意:我已尝试将条件设置为-1。

有人可以弄清楚发生了什么吗?

2 个答案:

答案 0 :(得分:2)

由于这有助于您解决问题,因此您的最新评论表明this bug was responsible.

  

ReadMultiStream类上的两个方法“read”违反了已实现的合同   接口java.io.InputStream。 Javadoc说,-1应该返回   流的结尾,但ReadMultiStream类仅为第一次调用返回-1   (在流的末尾),所有后续调用都会抛出异常。   可以通过在Web上发送字符流来模拟此问题   服务并将其包装到客户端的java.io.BufferedReader。什么时候   流不以新行字符结束,然后是方法的常用用法   readLine失败。

     

修复版本:2.2.6

答案 1 :(得分:0)

感谢@Jonathan Drapeau,@ 盒装 |问题被确定了。 ReadMultiStream违反了java.io.InputStream接口。

解决方案相当简单,服务器端:

@POST
@Consumes({ MediaType.MULTIPART_FORM_DATA })
@Produces({ MediaType.APPLICATION_JSON })
public Response uploadStorageFile(@Context UriInfo ui, @Context HttpHeaders hh,     @FormDataParam("file") 
InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail){
System.out.println(uploadedInputStream.getClass().getName());
String uploadedFileLocation = fileDetail.getFileName();
long size = fileDetail.getSize();
// save it
try {
            //test.jpg for test purposes
    OutputStream out = new FileOutputStream(new File("test.jpg")); 
    IOUtils.copy(uploadedInputStream, out);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

String output = "File uploaded to : ";

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

}

正如我在问题中提供的那样,多个教程正在教导输入流(服务器端),因此我不知道它是否是最近的错误。