为什么我的图像出现乱码?
-
09-06-2019 - |
题
我有一些 Java 代码,使用 servlet 和 Apache Commons FileUpload 将文件上传到设置的目录。它对于字符数据(例如文本文件)但图像文件出现乱码。我可以打开它们,但图像看起来不正常。这是我的代码:
服务程序
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String customerPath = "\\leetest\\";
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
// Parse the request
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
if (item.isFormField()) {
// Form field. Ignore for now
} else {
BufferedInputStream stream = new BufferedInputStream(item
.openStream());
if (stream == null) {
LOGGER
.error("Something went wrong with fetching the stream for field "
+ name);
}
byte[] bytes = StreamUtils.getBytes(stream);
FileManager.createFile(customerPath, item.getName(), bytes);
stream.close();
}
}
}
} catch (Exception e) {
throw new UploadException("An error occured during upload: "
+ e.getMessage());
}
}
StreamUtils.getBytes(stream) 看起来像:
public static byte[] getBytes(InputStream src, int buffsize)
throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byte[] buff = new byte[buffsize];
while (true) {
int nBytesRead = src.read(buff);
if (nBytesRead < 0) {
break;
}
byteStream.write(buff);
}
byte[] result = byteStream.toByteArray();
byteStream.close();
return result;
}
最后 FileManager.createFile 看起来像:
public static void createFile(String customerPath, String filename,
byte[] fileData) throws IOException {
customerPath = getFullPath(customerPath + filename);
File newFile = new File(customerPath);
if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}
FileOutputStream outputStream = new FileOutputStream(newFile);
outputStream.write(fileData);
outputStream.close();
}
谁能发现我做错了什么吗?
欢呼,李
解决方案
我不喜欢的一件事是 StreamUtils.getBytes() 的这个块中:
1 while (true) {
2 int nBytesRead = src.read(buff);
3 if (nBytesRead < 0) {
4 break;
5 }
6 byteStream.write(buff);
7 }
在第 6 行,无论读入多少字节,它都会写入整个缓冲区。我不相信情况永远如此。像这样会更正确:
1 while (true) {
2 int nBytesRead = src.read(buff);
3 if (nBytesRead < 0) {
4 break;
5 } else {
6 byteStream.write(buff, 0, nBytesRead);
7 }
8 }
请注意第 5 行的“else”以及第 6 行的两个附加参数(数组索引起始位置和要复制的长度)。
我可以想象,对于较大的文件(例如图像),缓冲区会在填充之前返回(也许它正在等待更多)。这意味着您会无意中写入保留在缓冲区尾部的旧数据。假设缓冲区 > 1 字节,几乎肯定会在 EoF 的大部分时间发生这种情况,但 EoF 的额外数据可能不是导致损坏的原因......这只是不可取的。
其他提示
我只想用 公共IO 然后你可以做一个 IOUtils.copy(InputStream, OutputStream);
它还有许多其他有用的实用方法。
您确定图像没有出现乱码或者您没有在传入过程中丢失一些数据包吗?
我不知道它有什么区别,但方法签名似乎不匹配。这 getBytes()
你调用的方法 doPost()
方法只有一个参数:
byte[] bytes = StreamUtils.getBytes(stream);
而您包含的方法源有两个参数:
public static byte[] getBytes(InputStream src, int buffsize)
希望有帮助。
您能否对原始文件和上传的文件执行校验和,看看是否有任何直接差异?
如果有,那么您可以查看执行比较,以确定文件中缺少更改的确切部分。
浮现在脑海中的是流的开始或结束,或者字节序。