HttpURLConnection发送post请求上传文件,文件为空?

hebiqibinqu 2010-07-20 09:56:49
我用HttpURLConnection来上传文件,上传来的word文件里面有字符,却什么也不显示,空白的,这是怎么一回事儿啊?

我代码如下:
String filename = "D:/ha.txt";
try {
String BOUNDARY = "---------7d4a6d158c9"; // 数据分隔线
URL url = new URL("http://127.0.0.1/JobPro/uploadprov.do");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);

OutputStream out = new DataOutputStream(conn.getOutputStream());
File file = new File(filename);

StringBuilder sb = new StringBuilder();
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"myfile\";filename=\""+ file.getName() + "\"\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();
out.write(data);
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytesOut = 0;
byte[] bufferOut = new byte[1024];
while ((bytesOut = in.read()) != -1) {
out.write(bufferOut, 0, bytesOut);
}

out.write(end_data);
out.flush();
out.close();
in.close();

// 定义BufferedReader输入流来读取URL的响应
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

} catch (Exception e) {
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
}
...全文
524 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hebiqibinqu 2010-07-23
  • 打赏
  • 举报
回复
呵呵,,谢谢你的回帖。。你搞复杂了。。
其实是我一句代码写错了。。
应该是
byte[] bufferOut = new byte[1024];
while ((bytesOut = in.read(1024)) != -1) {
out.write(bufferOut, 0, bytesOut);
}
read里少了个1024
牛叔 2010-07-20
  • 打赏
  • 举报
回复
前面
  document.all.FramerControl1.HttpInit();
// document.all.FramerControl1.HttpAddPostString("RecordID",nowtime);
// document.all.FramerControl1.HttpAddPostString("UserID",username);
// document.all.FramerControl1.HttpAddPostString("FileName",filename);//不能自定义参数,否则保存文件成乱码,定义其它参数貌似在后台需要截取出来,否则文件编码不对。
// document.all.FramerControl1.HttpAddPostString("SavePath",savepath);
document.all.FramerControl1.HttpAddPostCurrFile("FileData", "");
document.all.FramerControl1.HttpPost(path);



后面是c#的,你改一下

 string newFile = context.Server.MapPath("..") + context.Request.Params["FileName"];
if (context.Request.Files.Count == 0)
{
context.Response.Write("没有文件!");
return;
}

if (context.Request.Files[0].ContentLength == 0)
{
context.Response.Write("保存失败!");
return;
}

//定义保存文件的物理路径
//string strTmpPath = System.IO.Path.GetDirectoryName(Page.Request.PhysicalPath);

//定义保存文件的服务器路径
// string strUpLoadFilePath = strTmpPath + @"\doc\";

//string fullFileName = strUpLoadFilePath + strFileName + "." + strFileType;

context.Request.Files[0].SaveAs(newFile);
context.Response.Write("保存成功!");
context.Response.Flush();
// context.Response.End();
hebiqibinqu 2010-07-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ymdcr 的回复:]

去掉这些
前面也别传过来
你直接作为请求参数
不要作为控件里面的参数
[/Quote]

不太明白,去掉了更不行吧
牛叔 2010-07-20
  • 打赏
  • 举报
回复
去掉这些
前面也别传过来
你直接作为请求参数
不要作为控件里面的参数
牛叔 2010-07-20
  • 打赏
  • 举报
回复
你用的dsoframer?
sb.append("--");  
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"myfile\";filename=\""+ file.getName() + "\"\r\n");
sb.append("Content-Type:application/octet-stream\r\n\r\n");


你把这些去掉试试
还有dsoframer貌似不支持docx的上传
weboffice支持,但是存到后台也会成为doc

81,122

社区成员

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

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