Java:使用Restlet + Apache Commons FileUpload二进制文件上传 [英] Java: Binary File Upload using Restlet + Apache Commons FileUpload

查看:159
本文介绍了Java:使用Restlet + Apache Commons FileUpload二进制文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 Restlet 2.3 的REST API,需要为其实现文件上传功能. 问题是,当有人使用POST(具有multipart/form-data Content-Type)上传文件时,该文件将以另一种编码到达服务器. 为了对此进行测试,我在Unix Terminal中打印了原始文件的内容,然后再次打印了该文件,然后使用 Apache Commons FileUpload (与该示例几乎相同的代码

I have a REST API with Restlet 2.3 and need to implement a file-uploading functionality to it. The problem is that, when someone uploads a file using a POST (with a multipart/form-data Content-Type), the file reaches the server with another encoding. To test this, I printed the contents of the original file in a Unix Terminal and then printed it again before parsing the requet with Apache Commons FileUpload (with almost the same code of this example http://restlet.com/technical-resources/restlet-framework/guide/2.2/extensions/fileupload). Both printed contents are very similar, but the original file has less characters, so i assume that my Java server is using the wrong encoding to interpret the file.

我发送的文件是PNG图片.使用文本文件,服务器可以正常运行,但是当我发送照片或任何二进制文件时,就会出现问题.

The file I sent is a PNG image. With text files the server works perfectly, but when I send photos or any binary file, the problem appears.

推荐答案

上传图像文件时,我遇到了类似的问题.这就是我固定的方式.在我的情况下,问题是从输入流读取的数据.由于它是从套接字读取的,因此不能保证您将充满阵列的完整缓冲区.因此,在将数据写入输出缓冲区/文件之前,应检查数据大小.这是我的代码,希望对您有所帮助.也可在存储库 https://github.com/esabilbulbul/中获得java-servlet-fileupload/blob/master/README.md

I had the similar problem when uploading the image file. This is how I fixed. The problem was in my case the data read from the inputstream. As it is reading from a socket no guarantee that you will have the full buffer of your array filled. Therefore you should check your data size before writing it to the outputbuffer/file. Here is my code hope it helps. Also available in repository https://github.com/esabilbulbul/java-servlet-fileupload/blob/master/README.md

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
upload.setHeaderEncoding("UTF-8");

// Parse the request
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) 
{
    FileItemStream item = iter.next();
    String name = item.getFieldName();

    //InputStream attachmentStream = item.openStream();
    //byte[] attachmentBytes = ByteStreams.toByteArray(attachmentStream);


    //InputStream stream = item.getInputStream();
    InputStream stream = item.openStream();

    if (item.isFormField()) 
    {
        //System.out.println("Form field " + name + " with value " + Streams.asString(stream) + " detected.");
    }
    else
    {
        System.out.println("File field " + name + " with file name "+ item.getName() + " detected.");

        // Process the input stream
        FileOutputStream fout= new FileOutputStream ("c:\\" + item.getName());
        BufferedOutputStream bout= new BufferedOutputStream (fout);
        BufferedInputStream bin= new BufferedInputStream(stream);
        byte buf[] = new byte[2048];
        int len=0;
        while ((len = bin.read(buf)) > 0)//((bin.read(buf)) != -1)
        {
            bout.write(buf, 0, len);
            if (len<2048)
                len = len;
        }
        bout.close();
        bin.close();
    }        
}

这篇关于Java:使用Restlet + Apache Commons FileUpload二进制文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆