Google app engine gae上的Servlet删除jpeg图像
我希望编写一个在GAE中运行的servlet。此servlet希望上载图像并将其发送到电子邮件地址。代码如下:Google app engine gae上的Servlet删除jpeg图像,google-app-engine,file-upload,jpeg,Google App Engine,File Upload,Jpeg,我希望编写一个在GAE中运行的servlet。此servlet希望上载图像并将其发送到电子邮件地址。代码如下: ServletFileUpload upload = new ServletFileUpload(); FileItemIterator iterator = upload.getItemIterator(request); while (iterator.hasNext()) { FileItemStream itemStream = iterator.nex
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
FileItemStream itemStream = iterator.next();
is = itemStream.openStream();
if (itemStream.isFormField()){
String fieldname = itemStream.getFieldName();
if (fieldname.equals("Destinatar")){
destination = Streams.asString(is);
};
if (fieldname.equals("Mesaj")) {
message = Streams.asString(is);
};
if (fieldname.equals("Subject")) {
Subject = Streams.asString(is);
};
} else {
filename = FilenameUtils.getName(itemStream.getName());
contentFile = Streams.asString(is);
}
}
..........
............
...........
MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName(filename);
ds = new ByteArrayDataSource(contentFile.getBytes() , "image/jpeg");
attachment.setDataHandler(new DataHandler(ds));
multipart.addBodyPart(attachment);
..............
目标邮箱接收jpeg图像-文件名和尺寸正确,就像客户端一样-但浏览器无法理解内容,它无法像jpeg图像一样识别。
你有什么想法吗?有什么问题吗?
谢谢
Aurel您正在将二进制数据流转换为行中的字符串
contentFile = Streams.asString(is);
不要这样做。此转换使用字符集并将字节解码为字符,但肯定会失败,因为流不包含此字符集的有效字符。如果是二进制文件,则将其存储为二进制文件(存储到流或字节数组中):
InputStream fileContent;
// ...
else {
filename = FilenameUtils.getName(itemStream.getName());
fileContent = is;
}
// ...
ds = new ByteArrayDataSource(fileContent, "image/jpeg");
attachment.setDataHandler(new DataHandler(ds));