【问题标题】:Unable to getImage from request.getInputStream()无法从 request.getInputStream() 获取图像
【发布时间】:2013-03-08 18:23:48
【问题描述】:

我正在尝试从来自移动应用程序的请求属性中获取图像字节。

向上游发送的图像格式为 BitMap。 以 Base64 格式编码。

如何获取 Web 应用程序中的字节数? 我在我的 Web 应用程序中使用以下代码 sn-p 来从移动应用程序请求中获取数据。 我对代码感到震惊。我不确定我是对还是错。

byte[] line             = null;

    int noOfBytesRead;
    StringBuffer content    = new StringBuffer();

    HashMap<String, Object> lResultMap  =   null ;

    try 
    {
        log.info("request getInputStream: " + request.getInputStream());
        ServletInputStream sis  = request.getInputStream();

        line                    = new byte[128];
        noOfBytesRead           = sis.readLine(line, 0, 128);
        log.info("noOfBytesRead :1 " + noOfBytesRead);

        if (noOfBytesRead < 3)
            return null;

        noOfBytesRead           = sis.readLine(line, 0, 128); // Reads the content disposition and form name and filename

        log.info("line :"+line+" adn the noOfBytesRead : 2 : "  +   noOfBytesRead);

        int numBytesToRead      = noOfBytesRead;
        int availableBytesToRead;

        while ((availableBytesToRead = sis.available()) > 0) 
        {
            byte[] bufferBytesRead;
            bufferBytesRead     = availableBytesToRead >= numBytesToRead ? new byte[numBytesToRead] : new byte[availableBytesToRead];
            sis.read(bufferBytesRead);
            content.append(new String(bufferBytesRead, Charset.forName("iso-8859-1")));
        }
        sis.close();
        log.info("The bytes is  : " +content.toString());
        byte[] image        =   null;
        image   =   content.toString().getBytes(Charset.forName("iso-8859-1")) ;
        log.info("image : "  +image);


        Base64Decoder decoder = new Base64Decoder();  
        byte[] imgBytes = decoder.decode(content.toString());
        log.info("imgBytes : decoded byted" + imgBytes);        
        lResultMap          =   new HashMap<String, Object>();

        lResultMap.put("content"    , imgBytes );
        log.info("content string buffer :  " +content);
        log.info("lResultMap : "  +lResultMap);
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
        log.log(Level.SEVERE, e.getMessage(), e);
    }

【问题讨论】:

    标签: java android google-app-engine jakarta-ee


    【解决方案1】:

    从您的代码中很难理解,因为它没有格式化,但这是我的简单解决方案

    URL url = new URL(contentUrl);
            URLConnection conn = url.openConnection();
            InputStream inStr = conn.getInputStream();
            BufferedInputStream buffInStr = new BufferedInputStream(inStr);
            // Need to check the size of Byte Array Buffer.
            ByteArrayBuffer baf = new ByteArrayBuffer(500);
            int current = 0;
            while ((current = buffInStr.read()) != -1) {
                baf.append((byte) current);
            }
            byte[] array = baf.toByteArray();
    

    在谷歌应用引擎上

    图像服务 API 接受任何受支持的图像文件格式的数据。据this page称,这些格式包括“JPEG、PNG、WEBP、GIF(包括动画GIF)、BMP、TIFF和ICO”。图像服务不提供从头开始创建新图像数据的方法。但是您可以使用图形库以一种可接受的数据格式生成图像,然后使用该服务将其转换为另一种格式,或对其进行转换。当然,根据图形库的不同,你可能可以直接从库中获取最终的图像,而不是使用服务。

    要提供图像,只需为您使用的数据格式设置适当的 Content-Type 标头,然后将图像数据的字节写入响应的输出流:

    // byte[] pngData = ...
    resp.addHeader("Content-Type", "image/png");
    resp.getOutputStream().write(pngData);
    

    如果您想尝试在没有库的情况下生成图像数据,BMP 格式可能是最简单的。您可以使用图像服务将其转换为 PNG。

    【讨论】:

    • 感谢您的回答。很抱歉格式不好。我是新来使用这些代码的,你能告诉我 contenturl 是什么意思吗?
    • Contenturl 是一个字符串 url,如果这解决了你的问题,你需要从那里下载图片,你能接受我的回答吗
    • 我正在从移动应用程序获取图像作为字节我应该从请求中获取字节,它是一个 appengine 项目。如何从请求中以字节形式获取图像?
    • 我没明白你想说什么
    • 抱歉,我从手机上传图像并调用接收字节并创建图像的网络服务。我遇到了问题
    猜你喜欢
    • 2012-06-12
    • 2020-10-22
    • 2015-02-14
    • 2022-01-20
    • 2017-01-29
    • 2018-01-01
    • 2017-04-13
    • 2020-08-16
    • 1970-01-01
    相关资源
    最近更新 更多