【问题标题】:Servlet on gae delevering a jpeg image游戏中的 Servlet 提供 jpeg 图像
【发布时间】:2011-04-28 12:22:08
【问题描述】:

我希望编写一个 servlet 以在 GAE 中运行。此 servlet 想要上传图像并将其发送到电子邮件地址。这是代码:

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 图像那样识别。 你知道有什么问题吗? 谢谢, 奥雷尔

【问题讨论】:

    标签: google-app-engine file-upload jpeg


    【解决方案1】:

    您正在将二进制数据流转换为字符串

    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));
    

    【讨论】:

      猜你喜欢
      • 2011-02-21
      • 2019-06-20
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      • 2012-04-12
      • 1970-01-01
      相关资源
      最近更新 更多