HttpUrlConnection的POST方法如何向一个带有参数的地址上传文件?

时间:2023-01-21 13:40:41
我需要向一个类似于http://www.xxxxx.com?id=10001这样的网址上传一个文件

我也知道POST方法的话 参数要写在请求正文之中  

只是不知道是在哪行代码中写入   求高手指导


String urlPath = "http://www.xxxxx.com";
URL url = new URL(urlPath);// 得到网址
String BOUNDARY = "---------7d4a6d158c9";// 设置分隔线
byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();// 定义最后数据分隔线
HttpURLConnection conn = (HttpURLConnection) url.openConnection();//获得连接
// 参数设置
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
OutputStream outputStream = conn.getOutputStream();//获得输出流
StringBuilder sb = new StringBuilder();
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"file1\";filename=\""
+ file.getName() + "\"\r\n");
sb.append("Content-Type:application/octet-stream\r\n\r\n");
sb.append(end_data);
byte[] data = sb.toString().getBytes();
outputStream.write(data);

// 开始上传
int outSize;
byte[] outBuff = new byte[1024];
while ((outSize = fileInputStream.read(outBuff, 0, 1024)) > 0) {
outputStream.write(outBuff, 0, outSize);// 开始上传
}
outputStream.flush();
outputStream.close();
fileInputStream.close();

9 个解决方案

#1


HttpClient自带支持的,不需要你这么幸苦去搞。。。Google下大把实例代码:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost");
// 一个本地的文件
FileBody bin = new FileBody(new File("d:/BIMG1181.JPG"));
// 多部分的实体
MultipartEntity reqEntity = new MultipartEntity();
// 增加
reqEntity.addPart("bin", bin);
// 设置
httppost.setEntity(reqEntity);
System.out.println("执行: " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();

#2


引用 1 楼  的回复:
HttpClient自带支持的,不需要你这么幸苦去搞。。。Google下大把实例代码:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost");
// 一个本地的文件
FileBody bin = new FileBody(new Fil……

高手你在啊  请帮我看看下面这段代码吧  地址需要两个参数  我这样写貌似还是不对啊

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urlPath);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("file", cbFile);
Map<String, String> map = new HashMap<String, String>();
map.put("uuid", im);
map.put("token", token);
for (Map.Entry<String, String> entry : map.entrySet()) {
mpEntity.addPart(entry.getKey(), new StringBody(entry.getValue()));
}
post.setEntity(mpEntity);
HttpResponse response = client.execute(post);
String content = EntityUtils.toString(response.getEntity());
System.out.println(content);
client.getConnectionManager().shutdown();

#3


地址需要两个参数是啥意思?

不过你设置其它参数的方式有点问题啊:
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("uuid", new StringBody(im)); 
mpEntity.addPart("token", new StringBody(token));
mpEntity.addPart("file", cbFile);
post.setEntity(mpEntity);

#4


引用 3 楼  的回复:
地址需要两个参数是啥意思?

不过你设置其它参数的方式有点问题啊:
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("uuid", new StringBody(im)); 
mpEntity.addPart("tok……


就是类似于http://www.xxxxx.com?id=10001&name=abc这样的两个参数

#5


没必要带在URL里面啊,直接增加到 mpEntity 里面就好了:

mpEntity.addPart("id", new StringBody("10001")); 

或者你就直接组装URI。

#6


引用 5 楼  的回复:
没必要带在URL里面啊,直接增加到 mpEntity 里面就好了:

mpEntity.addPart("id", new StringBody("10001")); 

或者你就直接组装URI。


Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.commons.CommonsMultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.commons.CommonsMultipartFile]: no matching editors or conversion strategy found"

报这个错误  =。=

#7


咦?服务器端抛这个错误?

看起来像是类型识别错误,可能要这样修改:
MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
mpEntity.addPart("id", new StringBody("10001", "text/plain", Charset.forName("UTF-8")));

#8


膜拜一下大神

#9


跟帖顶。好贴。哈哈

#1


HttpClient自带支持的,不需要你这么幸苦去搞。。。Google下大把实例代码:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost");
// 一个本地的文件
FileBody bin = new FileBody(new File("d:/BIMG1181.JPG"));
// 多部分的实体
MultipartEntity reqEntity = new MultipartEntity();
// 增加
reqEntity.addPart("bin", bin);
// 设置
httppost.setEntity(reqEntity);
System.out.println("执行: " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();

#2


引用 1 楼  的回复:
HttpClient自带支持的,不需要你这么幸苦去搞。。。Google下大把实例代码:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost");
// 一个本地的文件
FileBody bin = new FileBody(new Fil……

高手你在啊  请帮我看看下面这段代码吧  地址需要两个参数  我这样写貌似还是不对啊

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urlPath);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("file", cbFile);
Map<String, String> map = new HashMap<String, String>();
map.put("uuid", im);
map.put("token", token);
for (Map.Entry<String, String> entry : map.entrySet()) {
mpEntity.addPart(entry.getKey(), new StringBody(entry.getValue()));
}
post.setEntity(mpEntity);
HttpResponse response = client.execute(post);
String content = EntityUtils.toString(response.getEntity());
System.out.println(content);
client.getConnectionManager().shutdown();

#3


地址需要两个参数是啥意思?

不过你设置其它参数的方式有点问题啊:
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("uuid", new StringBody(im)); 
mpEntity.addPart("token", new StringBody(token));
mpEntity.addPart("file", cbFile);
post.setEntity(mpEntity);

#4


引用 3 楼  的回复:
地址需要两个参数是啥意思?

不过你设置其它参数的方式有点问题啊:
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("uuid", new StringBody(im)); 
mpEntity.addPart("tok……


就是类似于http://www.xxxxx.com?id=10001&name=abc这样的两个参数

#5


没必要带在URL里面啊,直接增加到 mpEntity 里面就好了:

mpEntity.addPart("id", new StringBody("10001")); 

或者你就直接组装URI。

#6


引用 5 楼  的回复:
没必要带在URL里面啊,直接增加到 mpEntity 里面就好了:

mpEntity.addPart("id", new StringBody("10001")); 

或者你就直接组装URI。


Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.commons.CommonsMultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.commons.CommonsMultipartFile]: no matching editors or conversion strategy found"

报这个错误  =。=

#7


咦?服务器端抛这个错误?

看起来像是类型识别错误,可能要这样修改:
MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
mpEntity.addPart("id", new StringBody("10001", "text/plain", Charset.forName("UTF-8")));

#8


膜拜一下大神

#9


跟帖顶。好贴。哈哈