HttpURLConnection 的问题,100分求解,在线急等!!

dick4610 2012-02-17 10:27:10
小弟写了一个上传文件的程序,每次文件能上传成功,但就是要抛异常,程序如下,请高手帮我指出问题所在,谢谢!!

public void putFile(String fileServerURL, String fileName,String file)
{
try
{
URL url = new URL(fileServerURL+"/idf");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
String BOUNDARY = "---------------------------7d4a6d158c9"; // 分隔符
StringBuffer sb = new StringBuffer();
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\""+fileName+"\"; filename=\""+fileName+"\"\r\n");
sb.append("Content-Type: application/octet-stream\r\n\r\n");

byte[] data = sb.toString().getBytes();
byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();

conn.setRequestProperty("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.write(file.getBytes());
outStream.write(end_data);
outStream.flush();
outStream.close();


//得到返回的信息
InputStream is = conn.getInputStream();//此句直接就抛异常,为何????
System.out.println(is);
byte[] inbuf = new byte[1024];
int rn;
while((rn=is.read(inbuf,0,1024))>0)
{

logger.debug(new String(inbuf));

}
is.close();

}
catch(Exception ee)
{
logger.error("上传错误",ee);
}

}
...全文
81 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
贴出异常信息 看看呗
ypwDu 2012-02-17
  • 打赏
  • 举报
回复
是不是通信参数设定的不全?
下面的参数追加了吗?
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive ");
conn.setRequestProperty("content-type", "multipart/form-data; boundary=" + boundary + " ");

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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