来自java的multipart文件上传发布请求

编程入门 行业动态 更新时间:2024-10-12 12:26:12
本文介绍了来自java的multipart文件上传发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试制作一个程序,将图像上传到接受多部分文件上传的网络服务器。

I'm trying to make a program that uploads a image to a webserver that accepts multipart file-uploads.

更具体地说,我想向 http://发出http POST请求iqs.me 发送变量pic中的文件。

More specificly i want to make a http POST request to iqs.me that sends a file in the variable "pic".

我做了很多尝试,但我不知道我是不是甚至已经很近了。最难的部分似乎是获取HttpURLConnection来发出POST类型的请求。我得到的响应看起来像是一个GET。

I've made a lot of tries but i don't know if i've even been close. The hardest part seems to be to get a HttpURLConnection to make a request of the type POST. The response i get looks like it makes a GET.

(我想这样做没有任何第三方库)

(And i want to do this without any third party libs)

更新:非工作代码到此处(没有错误但似乎没有POST):

UPDATE: non-working code goes here (no errors but doesn't seem to do a POST):

HttpURLConnection conn = null; BufferedReader br = null; DataOutputStream dos = null; DataInputStream inStream = null; InputStream is = null; OutputStream os = null; boolean ret = false; String StrMessage = ""; String exsistingFileName = "myScreenShot.png"; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1*1024*1024; String responseFromServer = ""; String urlString = "iqs.local/index.php"; try{ FileInputStream fileInputStream = new FileInputStream( new File(exsistingFileName) ); URL url = new URL(urlString); conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setUseCaches(false); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); dos = new DataOutputStream( conn.getOutputStream() ); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"pic\";" + " filename=\"" + exsistingFileName +"\"" + lineEnd); dos.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0){ dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); fileInputStream.close(); dos.flush(); dos.close(); }catch (MalformedURLException ex){ System.out.println("Error:"+ex); }catch (IOException ioe){ System.out.println("Error:"+ioe); } try{ inStream = new DataInputStream ( conn.getInputStream() ); String str; while (( str = inStream.readLine()) != null){ System.out.println(str); } inStream.close(); }catch (IOException ioex){ System.out.println("Error: "+ioex); }

推荐答案

两件事:

  • 确保你调用setRequestMethod将HTTP请求设置为POST 。您应该收到警告,手动执行多部分POST请求很困难且容易出错。

  • Make sure you call setRequestMethod to set the HTTP request to be a POST. You should be warned that doing multipart POST requests by hand is difficult and error-prone.

    如果您在* NIX上运行,则工具 netcat 对于调试这些东西非常有用。运行 netcat -l -p 3000

    If you're running on *NIX, the tool netcat is very useful for debugging this stuff. Run netcat -l -p 3000

    并将程序指向端口3000;你会看到程序发送的确切内容(Control-C随后将其关闭)。

    and point your program to port 3000; you'll see exactly what the program is sending (Control-C to close it afterwards).

  • 更多推荐

    来自java的multipart文件上传发布请求

    本文发布于:2023-10-11 14:29:40,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1481971.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:文件上传   java   multipart

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!