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

a395885670 2012-05-02 08:08:38
我需要向一个类似于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();
...全文
8326 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangzhenyuzy 2014-09-24
  • 打赏
  • 举报
回复
跟帖顶。好贴。哈哈
白小四 2014-06-10
  • 打赏
  • 举报
回复
膜拜一下大神
MiceRice 2012-05-02
  • 打赏
  • 举报
回复
咦?服务器端抛这个错误?

看起来像是类型识别错误,可能要这样修改:
MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
mpEntity.addPart("id", new StringBody("10001", "text/plain", Charset.forName("UTF-8")));
a395885670 2012-05-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

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

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

或者你就直接组装URI。
[/Quote]

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"

报这个错误 =。=
MiceRice 2012-05-02
  • 打赏
  • 举报
回复
没必要带在URL里面啊,直接增加到 mpEntity 里面就好了:

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

或者你就直接组装URI。
a395885670 2012-05-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

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

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

就是类似于http://www.xxxxx.com?id=10001&name=abc这样的两个参数
MiceRice 2012-05-02
  • 打赏
  • 举报
回复
地址需要两个参数是啥意思?

不过你设置其它参数的方式有点问题啊:
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);

a395885670 2012-05-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

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

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost");
// 一个本地的文件
FileBody bin = new FileBody(new Fil……
[/Quote]
高手你在啊 请帮我看看下面这段代码吧 地址需要两个参数 我这样写貌似还是不对啊

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();
MiceRice 2012-05-02
  • 打赏
  • 举报
回复
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();

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧