request.getInputStream()得不到数据!!!!!!!!!!!!!!!!!!没有分了快来看看!!!!!!!

Godys9527 2013-08-08 06:05:13
首先对不起大家,没有分了只有10分了。

在用一个spring mvc做一个非常简单的导入导出xml的项目中的一个功能。
导出:从数据库中查询数据导出到xml中.
导入:从xml中解析数据,导入到数据库中。

导出已经解决。
导入的时候有问题。
就是用request.getInputStream()得到不数据。
先给代码

页面

<form id="sjwh_dr_form" action="${basepath}jcxx/sjwh/dr.action" method="post" >
<table class="tableys_yu" width="100%" border="0" cellpadding="0" cellspacing="1" style="border-spacing:1;">
<tr>
<td colspan="5" align="center">
<h1>导入XML</h1>
</td>
</tr>
<tr>
<td colspan="5">
<input id="sjwh_file" name="uploadFile" type="file" />
<input type="submit" value="导入XML" />
</td>
</tr>
</table>
</form>


到这个类中

@RequestMapping(value="dr")
public String dr(HttpServletRequest request,HttpServletResponse response,String navTabId,String uploadFile){
InputStream is = null;
try {
is = request.getInputStream();
// is.reset();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[]b=new byte[1024];
try {
is.read(b);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("is="+is.toString());
int i=uploadFile.lastIndexOf("\\");
uploadFile=uploadFile.substring(i+1, uploadFile.length());
// response.setContentType("multipart/form-data");
String message="";
// String fileName=uploadFile.getOriginalFilename();
// String path="C:\\"+fileName;
try {
FileUtils.copyInputStreamToFile(is, new File("C:\\", uploadFile));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
boolean isOK=sjwhServiceImpl.insertXML("C:\\"+uploadFile);
if(isOK){
message="导入成功!";
}else{
message="导入失败!";
}
File file=new File("C:\\"+uploadFile);
file.delete();
request.setAttribute("message",message);
return null;
}


上面的注释不用看了,是用spring 中@RequestParam MultipartFile uploadFile这个已经解决
下面是已经可以用的代码

@RequestMapping(value="dr")
public String dr(HttpServletRequest request,HttpServletResponse response,String navTabId,@RequestParam MultipartFile uploadFile){
String message="";
String fileName=uploadFile.getOriginalFilename();
String path="C:\\"+fileName;
try {
FileUtils.copyInputStreamToFile(uploadFile.getInputStream(), new File(path));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
message="导入失败!";
}
boolean isOK=sjwhServiceImpl.insertXML(path);
if(isOK){
message="导入成功!";
}else{
message="导入失败!";
}
File file=new File(path);
file.delete();
request.setAttribute("message",message);
return "forward:/pages/jcxx/sjwh/sjwh_liebiao.jsp";
}


注意到:
FileUtils.copyInputStreamToFile(uploadFile.getInputStream(), new File(path));
这个方法我想其实就是
FileUtils.copyInputStreamToFile(request.getInputStream(), new File(path));
应该是一样的吧,可以不 一样,因为报错了.
不知道 为什么不一样,所以我写了上面第2片代码,在
byte[]b=new byte[1024];
try {
is.read(b);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
这是直接rs.read(b)=-1,就说明没有数据,
所以生成的文件只有文件名,里面什么都没有。

想知道为什么request.getInputStream()没有获得到数据。
谢谢了,大家分享研究下.
...全文
433 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Godys9527 2013-08-09
  • 打赏
  • 举报
回复
引用 1 楼 oh_Maxy 的回复:
http://blog.csdn.net/jadyer/article/details/7575934 也是Spring MVC文件上传
先谢谢你,用SpringMVC可以上传, 我想不用的话,怎么上传
Godys9527 2013-08-09
  • 打赏
  • 举报
回复
我去,没有分大家就不来啊
oh_Maxy 2013-08-08
  • 打赏
  • 举报
回复
http://blog.csdn.net/jadyer/article/details/7575934 也是Spring MVC文件上传

23,404

社区成员

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

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