【问题标题】:multipart file-upload post request from java来自java的多部分文件上传发布请求
【发布时间】:2010-04-15 14:34:36
【问题描述】:

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

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

我做了很多尝试,但我什至不知道我是否已经接近了。最难的部分似乎是让 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);
  }

【问题讨论】:

  • 请贴出你的代码,否则我们很难帮你...
  • 我现在没有时间完成所有这些,但我之前已经发布了带有工作代码示例的答案,您可能会发现它也很有用:here 和关注向上here。为了减少所有冗长并简化所有工作,我强烈建议继续使用Apache HttpComponents HttpClient
  • 解决了,很好的例子。谢谢!
  • 您希望我将其作为答案发布以便您接受吗?

标签: java http multipartform-data


【解决方案1】:

两件事:

  1. 确保您call setRequestMethod to set the HTTP request to be a POST。应该警告您,手动执行多部分 POST 请求很困难且容易出错。

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

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

【讨论】:

  • 我将其设置为“POST”。现在没有运行 *NIX,但是很好的提示,下次会用到。
【解决方案2】:

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

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-24
    • 2014-02-04
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 2016-05-22
    • 2018-05-28
    • 2017-02-11
    相关资源
    最近更新 更多