我正在尝试从multipart / form获取文件。为此我使用的是HttpRequestHandler
这是我的班级
public class ResponseHandler implements HttpRequestHandler {
@Override
public void handle(HttpRequest request, HttpResponse response,
HttpContext httpContext) throws HttpException, IOException {
InputStream is = ((HttpEntityEnclosingRequest) request)
.getEntity().getContent();
//how extract file here
//stream consist other form data
}
public void saveImage(InputStream input,File file){
try {
OutputStream output = null;
try {
output = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
try {
final byte[] buffer = new byte[1024];
int read;
while ((read = input.read(buffer)) != -1)
output.write(buffer, 0, read);
output.flush();
} finally {
output.close();
}
} catch (Exception e) {
e.printStackTrace();
}
} finally {
}
}
}
这是multipart / form
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
我能够获得包含文件和表单数据的输入流。所以问题是如何从inputream中提取文件和其他数据。
如果我保存输入流,则文件看起来像是:
------ WebKitFormBoundaryzxxA1lnrlIK6B9ob 内容处理:表格数据; NAME = “fileToUpload”;文件名= “A.JPG” 内容类型:image / jpeg
ÿØÿàÿÀÿÀÿÀ234gy2ÿØÿÿÿÀÿÀÿÀ234gy2ÿÿÿàÿÀÿÀÿÀ234gy27ÿØÿàÀÿÀÿÀ234gy2ÿØÿàÿÀÿÀÿÀ234gy2ÿØÿàÿÀÿÀÿÀ234gy2 ..............................