日期:2014-05-19 浏览次数:20880 次
public class Dwr { public String upload(InputStream is,String filename) throws IOException { //WebContext可以获取HttpServlet的对象 WebContext wc = WebContextFactory.get(); HttpServletRequest req = wc.getHttpServletRequest(); String realpath = req.getSession().getServletContext().getRealPath("upload"); String fn = FilenameUtils.getName(filename); String filepath = realpath+"/"+fn; // FileUtils.copyInputStreamToFile(is, new File(filepath)); return filepath; } }
<script type="text/javascript" src='<%=request.getContextPath()%>/dwr/engine.js '></script> <script type="text/javascript" src='<%=request.getContextPath()%>/dwr/util.js '></script> <script type="text/javascript" src='<%=request.getContextPath()%>/dwr/interface/myDwr.js'></script> <script type="text/javascript"> function upload() { var file = dwr.util.getValue("myfile"); alert(dwr.util.getValue("myfile").value); alert(file); myDwr.upload(file,file.value,function(data){ alert(data); }); } </script> </head> <body> <input type="file" id="myfile"/> <input type="button" value="上传文件" onclick="upload()"/> </body>
<create creator="new" javascript="myDwr"> <param name="class" value="dwrTest.Dwr" /> <convert converter="bean" match="java.lang.StackTraceElement" />