Http PUT请求jpeg [英] Http PUT Request to jpeg

查看:99
本文介绍了Http PUT请求jpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到的HTTP PUT包括:

I received HTTP PUTs like:

PUT /photo HTTP/1.1
X-Apple-AssetKey: F92F9B91-954E-4D63-BB9A-EEC771ADE6E8
X-Apple-Transition: Dissolve
Content-Length: 462848
User-Agent: MediaControl/1.0
X-Apple-Session-ID: 1bd6ceeb-fffd-456c-a09c-996053a7a08c

<HERE COMES THE JPEG DATA>

尝试存储它,最终在 im == null例外

Socket s = server.accept();
    BufferedReader br = new BufferedReader(new InputStreamReader(
                    s.getInputStream()));

    String tag = br.readLine().split(" ")[1];

    System.out.println(tag);

    if (tag.contains("/photo")) {
        while (!br.readLine().equals(""));
            File file = new File("" + System.currentTimeMillis() + ".jpg");
            InputStream is = (s.getInputStream());
            BufferedImage bImageFromConvert = ImageIO.read(is); 
            ImageIO.write(bImageFromConvert, "jpg", file);
            System.out.println(file);
    }
    br.close();
    s.close();

所以我的想法是用BufferedReader剥离标题然后读取剩余的(包含jpeg)InputStream ,但我想BufferedReader不会影响InputStream的偏移量。那么如何跳过Header并编写jpeg呢?

So my idea was to strip off the header with the BufferedReader then reading the remaining (jpeg containing) InputStream, but I guess BufferedReader does not affect the offset of InputStream. So how can I skip the Header and write the jpeg?

推荐答案

我不建议这样做*,但如果你真的喜欢用低级方式做, HTTP标头部分始终以字符序列\\\\\\\ n结尾( \\\\ n 在规范中称为CRLF。如有疑问,请阅读 HTTP 1.1规范

I wouldn't recommend doing it this way*, but if you really like to do it the low-level way, the HTTP header part always ends with the character sequence "\r\n\r\n" (\r\n is referred to as CRLF in the spec). Whenever in doubt, read up on the HTTP 1.1 specification.

您需要做的就是搜索这种模式:

All you need to do, is to search for this pattern:

byte[] endOfHeader = "\r\n\r\n".getBytes(); // alt: getBytes(Charset.forName("ASCII"))

int endIndex = 0;
BufferedInputStream input = new BufferedInputStream(s.getInputStream());
input mark();

try {
    int read;
    while ((read = input.read()) != -1) {
        if (the last four bytes match endOfHeader) { // Left as an exercise ;-)
            break;
        }

        endIndex++;
    }
}
finally {
    input.reset();
}

// Now you have the end of header in endIndex
// You now have to re-read the header part using a character Reader 
// (BufferedReader is fine, just make sure you have the correct encoding)
// but make sure read *exactly* until endIndex before attempting to read further.

// The rest of input can now be read using ImageIO or similar, as you like.

// PS: Don't forget that the content may be chunked or zipped during 
//     transfer-encoding. You might need to handle this, unless you also
//     control the client sending the PUT request.
// PPS: I told you wouldn't recommend this ;-)






*)我首选的方法是使用嵌入式Jetty实例,并创建servlet来处理PUT。通过最少的设置和配置,它启动非常快。听起来有点矫枉过正,但我​​认为从长远来看,你可以省去一些痛苦。


*) My preferred way would be to use an embedded Jetty instance, and create servlet to handle the PUT. With minimal setup and configuration, it starts very fast. It might sound overkill, but you save yourself some pain in the long run, I think.

这篇关于Http PUT请求jpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆