错误提示 后台代码 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> dwr.xml配置 <create creator="new" javascript="myDwr"> <param name="class" value="dwrTest.Dwr" /> <convert converter="bean" match="java.lang.StackTraceElement" /> |
|
25分 |
我的DWR上传是这样写的
<%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8″%> <html> <head> <script type=”text/javascript”> function suffixalWord(){ var len = document.getElementById(“file”).value.length; if(document.getElementById(“file”).value.substring(len-3,len) != “doc”){ document.getElementById(“form”).reset(); alert(“上传的文档不是Word,请重新选择”); }else document.getElementById(“form”).submit(); } </script> </head> <%@page import=”java.io.File”%> //String word_file = new File(getServletContext().getRealPath(“/”).toString()+”UploadWord/”+fn); 自动获取url未完成 //将上传的文件保存到指定的文件夹 |
25分 |
给你一个demo
http://download.csdn.net/detail/s478853630/4360825 /** * 上传文件 * @param input 文件流 * @param filePath 文件保存的路径(物理路径) * @return Boolean 文件上传是否成功 */ public Boolean uploadFile(InputStream input, String filePath) { try { File file = new File(filePath);//新文件 OutputStream output = new FileOutputStream(file); try { byte[] buffer = new byte[1024]; int i = 0; while ((i = input.read(buffer)) != -1) { output.write(buffer, 0, i);//保存文件 } } catch (Exception e) { e.printStackTrace(); } finally { input.close(); output.close(); } return true; } catch (Exception e) { e.printStackTrace(); } return false; } /** * 一次上传多个文件 * @param inputs 文件流的数组 * @param fileNames 文件流对应的文件名 * @param folder 文件上传保存的文件夹(物理路径) * @return Integer 上传文件成功的个数 */ public Integer uploadFile(InputStream []inputs, String []fileNames, String folder) { Integer result = 0; if (null != inputs && inputs.length > 0 && null != fileNames && inputs.length == fileNames.length) { FileUtil.createFolder(folder); for (int i = 0; i < inputs.length; i++) { if (uploadFile(inputs[i], folder + fileNames[i])) { result++; } } } return result; } <%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib prefix="z" uri="/zl"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>dwr上传文件示例</title> <link rel="stylesheet" href="<z:contextPath/>/common_res/css/operate.css" type="text/css"></link> <script type="text/javascript" src="<z:contextPath/>/common_res/js/check.js"></script> <script type="text/javascript" src="<z:contextPath/>/dwr/interface/fileUpload.js"></script> <script type="text/javascript" src="<z:contextPath/>/dwr/engine.js"></script> <script type="text/javascript" src="<z:contextPath/>/dwr/util.js"></script> <script type="text/javascript"> /**这两个变量用来上传单个文件(图片)*/ var stream;// 文件流的全局变量 var filePath;// 文件路径的全局变量 /**上传图片*/ function uploadImg() { if (getV("txtImg") != "") { if (isImgFile("txtImg", "1")) { filePath = getV("txtImg"); var oldName = filePath.substring(filePath.lastIndexOf("\") + 1); setLoadGress("上传图片", "正在上传<font color=""green"">" + oldName + "</font>,请稍候...."); stream = dwr.util.getValue("txtImg"); fileUpload.getStreamLength(stream, function(result) { var fileName = getNowDate("2") + filePath.substring(filePath.lastIndexOf(".")); if (parseInt(result / (1024 * 1024)) < 5) { fileUpload.upload(stream, getV("hdfFolder"), fileName, function(result) { getO("imgFile").src = getV("hdfPath") + result + "?random=" + Math.random(); showDiv("divImg", "1"); closeLoadGress(); }); } else { closeLoadGress(); errorPormt("txtImg", "图片大小不能超过5M"); } }); } } else { errorPormt("txtImg", "请选择一张图片"); } } /**上传单个文件*/ function uploadFile() { if (getV("txtFile") != "") { filePath = getV("txtFile"); var oldName = filePath.substring(filePath.lastIndexOf("\") + 1); setPlanGress("上传文件", "正在上传<font color=""green"">" + oldName + "</font> ", "500", "0", "uploadFile1()"); var fileName = getNowDate("2") + filePath.substring(filePath.lastIndexOf(".")); fileUpload.upload(dwr.util.getValue("txtFile"), getV("hdfFolder"), fileName, function(result) { setV("hdfNewPath", result); }); } else { errorPormt("txtFile", "请选择一个文件"); } } var timerUpload; function uploadFile1() { if (getV("hdfNewPath") != "") { clearTimeout(timerUpload); showDiv("divEnvelop", "0"); showDiv("divPlanGress", "0"); var oldName = filePath.substring(filePath.lastIndexOf("\") + 1); alertWin("提示", 340, 110, "上传文件【" + oldName + "】成功!", "2", ""); } else { showDiv("divEnvelop", "1"); showDiv("divPlanGress", "1"); timerUpload = setTimeout("uploadFile1()", 100); } } /**这个变量用来上传多个文件(图片)*/ var filePaths;// 文件路径的全局变量 function moreUpload() { filePaths = new Array(); for (var i = 0; i < 5; i++) { if (getV("txtFile" + i) != "") { filePaths[i] = getV("txtFile" + i); } } if (filePaths.length > 0) { moreUpload1(0); } var streams = new Array(); var fileNames = new Array(); for (var i = 0; i < 5; i++) { if (getV("txtFile" + i) != "") { streams[i] = dwr.util.getValue("txtFile" + i); fileNames[i] = getNowDate("2") + new String(Math.random()).substring(3, 7) + getV("txtFile" + i).substring(getV("txtFile" + i).lastIndexOf(".")); } } fileUpload.moreUpload(streams, getV("hdfFolder"), fileNames, function(result) {}); } function moreUpload1(index) { var i = parseInt(index); if (i < filePaths.length) { var oldName = filePaths[i].substring(filePaths[i].lastIndexOf("\") + 1); var callback = (i == filePaths.length -1) ? ""alertWin("提示", 300, 110, "成功上传了"" + filePaths.length + ""个文件!", "2", "")"" : ""moreUpload1(\"""" + (i + 1) + ""\"")""; setPlanGress("上传文件", "正在上传<font color=""green"">" + oldName + "</font> ", "40", "3", callback); } } /**批量上传时不能选择重复的文件*/ function resetFile(index) { var num = 0; for (var i = 0; i < 5; i++) { if (i != parseInt(index)) { if (getV("txtFile" + i) == getV("txtFile" + index)) { clearFile("txtFile" + index); break; } } } } /**清空文件域选择的文件(只能在IE使用)*/ function clearFile(id) { var index = getO(id).outerHTML.indexOf("value"); if (index != -1) { getO(id).outerHTML = getO(id).outerHTML.substring(0, index) + " />"; } } </script> </head> <body style="padding:20px;margin:20px;"> <input type="hidden" id="hdfPath" value="<z:contextPath/>"/> <input type="hidden" id="hdfFolder" value="/upload/temp/"/> <input type="hidden" id="hdfNewPath"/> <label>选择图片:</label><input type="file" id="txtImg" onchange="uploadImg()" onkeydown="notFileInput(event)"/><br/> <div id="divImg" style="border:1px solid #5aa2ef;display:none;width:300px;height:260px;" class="divScroll"> <img id="imgFile"/> </div> <label>选择文件:</label><input type="file" id="txtFile"/> <input type="button" value="上传" style="width:auto;" onclick="uploadFile()"/><br/> <% for (int i = 0; i < 5; i++) { %> <label>选择文件:</label><input type="file" id="txtFile<%=i%>" onchange="resetFile(""<%=i %>"")"/><br/> <% } %> <input type="button" value="批量上传" style="width:auto;" onclick="moreUpload()"/><br/> </body> </html> |
运行的时候会报错:org.directwebremoting.dwrp.BaseCallMarshaller —Erroring: batchId[2] message[java.lang.IllegalArgumentException: Missing method or missing parameter converters],可能是在JSP页面uploadFile并不是InputStream类型,但是在DWR的代码中直接当InputStream类型使用了,大虾们怎么解决呢? 我是用的dwr2.0版的
|
|
楼上的解决了么,我这边也是这个问题
|
|
解决了么? |