ContentType问题与commons-upload和httpcomponent客户端 [英] ContentType issue with commons-upload and httpcomponent client

查看:215
本文介绍了ContentType问题与commons-upload和httpcomponent客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



$ p $ H $ p $ H $ p $ HttpPost HttpPost = new HttpPost(myURL) ;
httppost.setHeader(Content-type,
multipart / form-data; boundary = stackoverflow);
httppost.setHeader(Accept,text / xml);
MultipartEntity reqEntity = new multipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE,stackoverflow,
Charset.forName(UTF-8));
FileBody bin = new FileBody(myFile);
reqEntity.addPart(File,bin);
httppost.setEntity(reqEntity);
HttpResponse响应= client.execute(httppost);

在服务器端,有一个

<$ pre $
$ b $ p
$ b $ p $ {code $>请求:
$ b

  FileItemFactory factory = new DiskFileItemFactory(204800,new File(
uploadDirectory));
ServletFileUpload fileUpload = new ServletFileUpload(factory);
尝试{
List< FileItem> items = fileUpload.parseRequest(request);
Iterator< FileItem> itemIterator = items.iterator();
while(itemIterator.hasNext()){
FileItem item = itemIterator.next();
....

它工作正常,但问题在于我的FileItem的Content-type是空的,我有一个NullPointerException以后。然而,当我做客户端的bin.getContentType()我得到文本/ XML。

有人知道什么时候这种内容类型丢失,如何解决?

解决方案

您正在使用错误的模式。在BROWSER_COMPATIBLE模式下,内容类型不会发送到服务器。您需要使用STRICT模式发送content_type。



然而,STRICT可能不适合您,因为某些网络服务器不喜欢它。



FileBody的默认内容类型是application / octet-stream。你可以像这样改变它,

  FileBody bin = new FileBody(new File(myFile),text / html); 


I am trying to develop a Http client which uploads a file with httpcomponents:

HttpPost httppost = new HttpPost(myURL);
httppost.setHeader("Content-type",
       "multipart/form-data; boundary=stackoverflow");
httppost.setHeader("Accept", "text/xml");
MultipartEntity reqEntity = new multipartEntity(
       HttpMultipartMode.BROWSER_COMPATIBLE,"stackoverflow",
       Charset.forName("UTF-8"));
FileBody bin = new FileBody(myFile);
reqEntity.addPart("File", bin);
httppost.setEntity(reqEntity);
HttpResponse response = client.execute(httppost);

On server side, there is a

doPost(HttpServletRequest request, HttpServletResponse response)

method which parse the request :

FileItemFactory factory = new DiskFileItemFactory(204800, new File(
                    uploadDirectory));
ServletFileUpload fileUpload = new ServletFileUpload(factory);
try {
     List<FileItem> items = fileUpload.parseRequest(request);
     Iterator<FileItem> itemIterator = items.iterator();
     while (itemIterator.hasNext()) {
          FileItem item = itemIterator.next();
           ....

It works fine but the problem is that my FileItem's Content-type is null and I have a NullPointerException later. However when I do bin.getContentType() on client side I get "text/xml".

Does someone know when this Content type is lost and how to fix that?

解决方案

You are using the wrong mode. In BROWSER_COMPATIBLE mode, the content-type is not sent to server. You need to use STRICT mode to send content_type.

However, STRICT may not work for you because some web servers don't like it.

The default content-type for FileBody is application/octet-stream. You can change it like this,

  FileBody bin = new FileBody(new File(myFile), "text/html");

这篇关于ContentType问题与commons-upload和httpcomponent客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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