繁体   English   中英

Java:使用Restlet + Apache Commons FileUpload二进制文件上传

[英]Java: Binary File Upload using Restlet + Apache Commons FileUpload

我有一个带有Restlet 2.3的REST API,需要为其实现文件上传功能。 问题是,当有人使用POST(具有multipart / form-data Content-Type)上传文件时,该文件将以另一种编码到达服务器。 为了测试这一点,我在Unix终端中打印了原始文件的内容,然后在使用Apache Commons FileUpload解析记录之前再次打印了该文件(使用与该示例几乎相同的代码http://restlet.com/technical-resources/ restlet-framework / guide / 2.2 / extensions / fileupload )。 两种打印的内容都非常相似,但是原始文件的字符较少,因此我认为我的Java服务器使用错误的编码来解释文件。

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

我不知道您究竟如何检查收到的内容。 首先,您应该在多部分请求的内容中检查用于文件部分的内容类型。 对于JPG图片,您应该有类似的内容:

-----------------------------75956101888331271337088331
Content-Disposition: form-data; name="fileToUpload"; filename="myimage.jpg" 
Content-Type: image/jpeg

其次,我不知道您实际上是如何编写收到的内容的。 Apache的百科全书IO带来了一个实用方法IOUtils.copy ,提供了一个简单的解决方案在一个写OutputStream从接收到的内容InputStream 查看如何在您的上下文中使用ti:

while (fileIterator.hasNext()) {
    FileItemStream fi = fileIterator.next();
    if (fi.getFieldName().equals("fileToUpload")) {
        FileOutputStream fos = new FileOutputStream(
              "output"+File.separator+fi.getFieldName());
        IOUtils.copy(fi.openStream(), fos);
        fos.close();
    }
}

IMO,编码方面仅适用于文本,不适用于二进制内容。

希望对您有帮助,蒂埃里

我实际上通过使用Google的ByteStreams类解决了它:

while (fileIterator.hasNext()) {
    FileItemStream fi = fileIterator.next();
    if (fi.getFieldName().equals(FILE_TO_UPLOAD)) {
        byte[] byteArray = ByteStreams.toByteArray(fi.openStream());
        result = new String(byteArray,Charset.forName("ISO-8859-1"));
    }
}

上传图片文件时,我遇到了类似的问题。 这就是我固定的方式。 在我看来,问题是从输入流读取的数据。 由于它是从套接字读取的,因此不能保证您将充满阵列的完整缓冲区。 因此,在将数据写入输出缓冲区/文件之前,应检查数据大小。 这是我的代码,希望对您有所帮助。 也可以在存储库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();
    }        
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM