DWR入门 (四)文件上传

DWR的文件上传有点繁琐,首先版本要使用3.0.RC2。
据我了解,DWR的版本更新还是比较慢的。 而且3.0.RC2没有被发布到maven中央服务器上,我们必须自己下载,加入本地仓库才能使用。
[url]http://directwebremoting.org/dwr/downloads/index.html#maven[/url]


这个网址[url]http://directwebremoting.org/jira/browse/DWR-331[/url]就是关于Chrome中DWR无法正常上传文件的具体错误信息以及解决方案。(其实IE也不行)


开始写代码。
首先在HelloDwr.java中加入以下代码:

	public String upload(InputStream is, String filename){
String fn=FilenameUtils.getName(filename);
try {
FileUtils.copyInputStreamToFile(is, new File("d:/"+fn));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fn;
}


然后写一个上传jsp页面:


<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.0.js"></script>
<script type="text/javascript" src="<%=path%>/dwr/engine.js"></script>
<script type="text/javascript"
src="<%=path%>/dwr/interface/HelloDwr.js"></script>


<script type="text/javascript">

$(function(){
$("#btn").on("click",upload);

});

function upload(){
var f=$("#uf");
var f2=document.getElementById("uf");
//alert(f);
//alert(f.val());
console.log(f);
HelloDwr.upload(f2,f2.value,function(data){
alert(data);
});



}


</script>

</head>

<body>

<input type="file" id="uf"/>
<input id="btn" type="button" value="上传"/>


</body>
</html>



[b]关于错误信息[/b]
1.如果dwr版本不正确,console会报错[b]Cannot set property 'batch' of null [/b]
google查询便会找到上面那个官方的Issue网址。

2. 如果没有加入commons.fileupload依赖包,便会报错:
[color=red]警告: Exception while processing batch
java.lang.UnsupportedOperationException: File uploads not supported
at org.directwebremoting.dwrp.UnsupportedFileUpload.parseRequest(UnsupportedFileUpload.java:41)
at org.directwebremoting.d wrp.Batch.parsePost(Batch.java:135)
at org.directwebremoting.dwrp.Batch.<init>(Batch.java:58)
at org.directwebremoting.dwrp.CallBatch.<init>(CallBatch.java:46)
at org.directwebremoting.dwrp.BaseCallHandler.handle(BaseCallHandler.java:74)
at org.directwebremoting.servlet.UrlProcessor.handle(UrlProcessor.java:120)
at org.directwebremoting.servlet.DwrServlet.doPost(DwrServlet.java:141)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:575)
[/color]


=========================
补充:
如果想把文件放入项目目录下,可以修改upload方法:

	public String upload(InputStream is, String filename){
WebContext wc=WebContextFactory.get();
HttpServletRequest req=wc.getHttpServletRequest();

//这里可以将上传文件放入webapp/img目录中.
String realPath=req.getSession().getServletContext().getRealPath("/img");
System.out.println("realpath= "+realPath);




String fn=FilenameUtils.getName(filename);
try {
FileUtils.copyInputStreamToFile(is, new File(realPath+fn));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fn;
}

运行之后,在myeclipse的webapp目录点刷新,便可看到上传文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值