Java中的多部分文件上传POST请求

3
我正在尝试制作一个程序,将图像上传到接受多部分文件上传的Web服务器。
更具体地说,我想向http://iqs.me发送一个包含在变量“pic”中的文件的HTTP POST请求。
我已经尝试了很多次,但我不知道是否接近成功。最难的部分似乎是获取HttpURLConnection以进行POST类型的请求。我收到的响应看起来像是GET请求。
(我想做到这一点,而不使用任何第三方库)
更新:下面是无法工作的代码(没有错误,但似乎没有执行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);
  }

请发布您的代码,否则我们几乎无法帮助您... - Péter Török
我现在没有时间去仔细研究这个问题,但我之前已经发布了带有可用代码示例的答案,你也许会觉得有用:这里 和后续 这里。为了减少冗余并简化工作,我强烈建议使用 Apache HttpComponents HttpClient - BalusC
解决了,非常好的例子。谢谢! - Martin
你想让我将其发布为答案,以便您可以接受它吗? - BalusC
2个回答

2

有两件事情:

  1. 确保你调用setRequestMethod方法将HTTP请求设置为POST。需要注意的是,手动执行multipart POST请求非常困难且容易出错。

  2. 如果你在*NIX上运行,请使用netcat工具进行调试。运行
    netcat -l -p 3000

    然后将程序指向端口3000;你将看到程序正在发送的内容(按Control-C关闭它)。


我将其设置为“POST”。现在没有运行*NIX,但是好的提示,以后会用到。 - Martin

0

我曾经使用过这个,在多部分文件上传中发现它很有用

File f = new File(filePath);
PostMethod filePost = new PostMethod(url);
Part[] parts = { new FilePart("file", f) };
filePost.setRequestEntity(new MultipartRequestEntity(parts,
filePost.getParams()));
HttpClient client = new HttpClient();
status = client.executeMethod(filePost);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接