来自java的multipart文件上传发布请求 [英] multipart file-upload post request from java

查看:285
本文介绍了来自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 http://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 = "http://iqs.local.com/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);
  }


推荐答案

两件事:


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

  1. 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文件上传发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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