来自java的多部分文件上传发布请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2646194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
multipart file-upload post request from java
提问by Martin
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.methat sends a file in the variable "pic".
更具体地说,我想向http://iqs.me发出一个 http POST 请求,该请求在变量“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.
我已经做了很多尝试,但我不知道我是否已经接近了。最难的部分似乎是获取 HttpURLConnection 以发出 POST 类型的请求。我得到的响应看起来像是 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):
更新:非工作代码放在这里(没有错误,但似乎没有执行 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);
}
回答by James Kingsbery
Two things:
两件事情:
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.
If you're running on *NIX, the tool netcatis 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).
确保您调用 setRequestMethod 将 HTTP 请求设置为 POST。应该警告您,手动执行多部分 POST 请求很困难且容易出错。
如果你在 *NIX 上运行,netcat工具对于调试这些东西非常有用。跑
netcat -l -p 3000
并将您的程序指向端口 3000;你会看到程序发送的确切内容(Control-C 之后关闭它)。
回答by Aashish Katta
I have used this and found it useful in multipart file upload
我用过这个,发现它在分段文件上传中很有用
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);