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

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

问题描述

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

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

servlet 部分使用 Apache Commons FileUpload 覆盖

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 似乎只能上传 multipart 文件,但不能上传 application/octect-stream.

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

但是对于客户端来说,没有 Multipart 类,在这种情况下,也不能使用任何 HttpClient 库.

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

其他选项可能是使用 HTTP 块上传,但我还没有找到一个明确的例子来说明如何实现这一点,特别是在 servlet 端.

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.

我的选择是:- 为http块上传实现一个servlet- 为 http 多部分创建实现一个原始客户端

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?

推荐答案

通过 HTTP 发送文件应该使用 multipart/form-data 编码进行.您的 servlet 部分很好,因为它已经使用 Apache Commons FileUpload 来解析 multipart/form-数据请求.

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.

然而,您的客户端部分显然不好,因为您似乎将文件内容原始写入请求正文.您需要确保您的客户端发送正确的 multipart/form-data 请求.具体如何执行取决于您用于发送 HTTP 请求的 API.如果它是普通的 java.net.URLConnection,那么您可以在 这个答案.如果您为此使用 Apache HttpComponents Client,那么这是一个具体的例子,取自 他们的文档:

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, taken from their documentation:

String url = "https://example.com";
File file = new File("/example.ext");

try (CloseableHttpClient client = HttpClients.createDefault()) {
    HttpPost post = new HttpPost(url);
    HttpEntity entity = MultipartEntityBuilder.create().addPart("file", new FileBody(file)).build();
    post.setEntity(entity);

    try (CloseableHttpResponse 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);

这可能会覆盖任何以前上传的同名文件.我建议使用 File#createTempFile() 代替.

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客户端通过POST上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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