Java Http Client通过POST上传文件 [英] Java Http Client to upload file over POST

查看:1191
本文介绍了Java Http Client通过POST上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个J2ME客户端,它必须使用HTTP将一个文件上传到一个Servlet。
$ b

servlet部分使用Apache Commons FileUpload进行覆盖



pre $ protected $ doPost(HttpServletRequest请求,HttpServletResponse响应)
{

ServletFileUpload upload = new ServletFileUpload();
upload.setSizeMax(1000000);

文件fileItems = upload.parseRequest(request);

//处理上传的项目
Iterator iter = fileItems.iterator(); (iter.hasNext()){
FileItem item =(FileItem)iter.next();
File file = new File(\files\\+ item.getName());
item.write(file);


code


$ b Commons Upload似乎只能上传多部分文件,但没有application / octect-stream。

但是对于客户端来说,没有Multipart类,在这种情况下,可以使用任何HttpClient库。 / p>

其他选项可以使用HTTP Chunk上传,但是我还没有找到一个清楚的例子来说明如何实现,特别是在servlet方面。



我的选择是:
- 为http chunk上传实现一个servlet
- 为http multipart创建实现一个原始客户端

我不知道如何实现上述选项。
任何建议?

解决方案

通过HTTP发送文件应该使用 multipart / form-data 编码。你的servlet部分是好的,因为它已经使用 Apache Commons FileUpload 来解析一个 multipart / form-data request。



然而,您的客户端部分显然不好,因为您似乎将文件内容写入请求正文。您需要确保您的客户端发送正确的 multipart / form-data 请求。究竟该怎么做取决于你用来发送HTTP请求的API。如果是普通的香草 java.net.URLConnection ,那么你可以在这个答案。如果您使用的是 Apache HttpComponents客户端,那么下面是一个具体的例子:

  HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost(url);

MultipartEntity entity = new MultipartEntity();
entity.addPart(file,new FileBody(file));
post.setEntity(entity);

HttpResponse response = client.execute(post);
// ...






Unrelated 到具体问题,您的服务器端代码中有一个错误:

  File file = new File ( \files\\ + item.getName()); 
item.write(file);

这可能会覆盖以前上传的同名文件。我建议使用 File#createTempFile() 取而代之。

  String name = FilenameUtils.getBaseName(item.getName()); 
String ext = FilenameUtils.getExtension(item.getName());
File file = File.createTempFile(name +_,。+ ext,new File(/ files));
item.write(file);


I'm developing a J2ME client that must upload a file to a Servlet using HTTP.

The servlet part is covered using Apache Commons FileUpload

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
{       

    ServletFileUpload upload = new ServletFileUpload();
    upload.setSizeMax(1000000);

    File fileItems = upload.parseRequest(request);

    // Process the uploaded items
    Iterator iter = fileItems.iterator();
    while (iter.hasNext()) {
        FileItem item = (FileItem) iter.next();
        File file = new File("\files\\"+item.getName());
        item.write(file);
    }
}

Commons Upload seems to be able to upload only multipart file, but no application/octect-stream.

But for the client side there are no Multipart classes, neither, in this case, is possible to use any HttpClient library.

Other option could be to use HTTP Chunk upload, but I haven't found a clear example of how this could be implemented, specially on the servlet side.

My choices are: - Implement a servlet for http chunk upload - Implement a raw client for http multipart creation

I don't know how to implement none of the above options. Any suggestion?

解决方案

Sending files over HTTP is supposed to take place using multipart/form-data encoding. Your servlet part is fine as it already uses Apache Commons FileUpload to parse a multipart/form-data request.

Your client part, however, is apparently not fine as you're seemingly writing the file content raw to the request body. You need to ensure that your client sends a proper multipart/form-data request. How exactly to do it depends on the API you're using to send the HTTP request. If it's plain vanilla java.net.URLConnection, then you can find a concrete example somewhere near the bottom of this answer. If you're using Apache HttpComponents Client for this, then here's a concrete example:

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);

MultipartEntity entity = new MultipartEntity();
entity.addPart("file", new FileBody(file));
post.setEntity(entity);

HttpResponse response = client.execute(post);
// ...


Unrelated to the concrete problem, there's a bug in your server side code:

File file = new File("\files\\"+item.getName());
item.write(file);

This will potentially overwrite any previously uploaded file with the same name. I'd suggest to use File#createTempFile() for this instead.

String name = FilenameUtils.getBaseName(item.getName());
String ext = FilenameUtils.getExtension(item.getName());
File file = File.createTempFile(name + "_", "." + ext, new File("/files"));
item.write(file);

这篇关于Java Http Client通过POST上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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