Java Http将请求放入jpeg
我收到了类似以下内容的HTTP文件:Java Http将请求放入jpeg,java,http,jpeg,Java,Http,Jpeg,我收到了类似以下内容的HTTP文件: 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 TH
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>
所以我的想法是用BufferedReader去掉标题,然后读取剩余的(包含jpeg的)InputStream,但我猜BufferedReader不会影响InputStream的偏移量。那么我如何跳过标题并编写jpeg呢 我不建议这样做*,但是如果你真的喜欢用低级的方式,HTTP头部分总是以字符序列
“\r\n\r\n”
结尾(\r\n
在规范中被称为CRLF)。如果有疑问,请仔细阅读
您只需搜索以下模式:
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。通过最少的设置和配置,它启动得非常快。这听起来可能有些过分,但我认为,从长远来看,这会给你自己带来一些痛苦。你真的需要编写底层套接字来使用HTTP吗?为什么不使用一个已经解决了这个问题的现有servlet容器(如Tomcat或Jetty)呢?对于这个简单的任务来说,这是一个过度的任务。@KanakSony也许。。关于这个答案你有什么问题吗?否则,您可能应该问一个单独的问题。实际上,我得到的图像的内容长度与图像的实际大小不同。我不知道为什么图像大小会增加。里面还加了什么?我怎么知道呢?@KanakSony这些不是关于这个答案的评论。相反,你应该编辑自己的问题以吸引更多的注意力。
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 ;-)