Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

http - multipart file-upload post request from java

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

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

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)

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 = "
";
  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);
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Two things:

  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.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...