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/扩展/文件上传).两个打印的内容非常相似,但原始文件的字符较少,所以我假设我的Java服务器使用了错误的编码来解释文件.
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屋!