当前位置: 代码迷 >> J2EE >> dwr 上传文件 出错 求高手解决下多谢(急)
  详细解决方案

dwr 上传文件 出错 求高手解决下多谢(急)

热度:533   发布时间:2016-04-21 22:14:17.0
dwr 上传文件 出错 求高手解决下谢谢(急)
错误提示 
2012-8-2 15:07:53 org.directwebremoting.util.CommonsLoggingOutput warn
警告: Marshalling exception: Missing method or missing parameter converters
2012-8-2 15:07:53 org.directwebremoting.util.CommonsLoggingOutput warn
警告: --Erroring: batchId[0] message[java.lang.IllegalArgumentException: Missing method or missing parameter converters]


后台代码

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" />



------解决方案--------------------
我的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>
  <body>
  <center>
    <h1>请选择word文档</h1>
    <form action="uploadWord.jsp" method="post" enctype="multipart/form-data"  id="form">
     <p>上传文件:<input type="file" name="file"  id="file">
     <p><input type="button" value="提交" onclick="suffixalWord()"/><input type="reset" value="重置"/></p>
    </form>
    </center>
  </body>
</html>


<%@page import="java.io.File"%>
<%@ page language="java" import="java.util.*,com.jspsmart.upload.*" pageEncoding="utf-8"%>
<html>
<body>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<h1>
<%
try {
mySmartUpload.initialize(pageContext);
mySmartUpload.service(request, response);
mySmartUpload.upload();
//得到第一个上传的文件名
String fn = mySmartUpload.getFiles().getFile(0).getFileName();

//String word_file = new File(getServletContext().getRealPath("/").toString()+"UploadWord/"+fn); 自动获取url未完成
//session.setAttribute("WordFile", word_file);//添加到session中
//session.setAttribute("WordFile", "E://Program Files/SoftwareDocument/Apache Software Foundation/Tomcat 6.0/webapps/AnalyticWord/UploadWord/"+fn);

//将上传的文件保存到指定的文件夹
mySmartUpload.save("UploadWord/");
out.print("已经成功上传了文件,请查看<a href=UploadWord/" + fn + ">这里</a>,看文件是否上传成功<br/>");
out.print("<a href=\"word.jsp\">网页模式查看Word文档</a>");
} catch (Exception e) {
session.setAttribute("WordFile", "");
out.print("上传Word文档失败,<a href=uploadWordfile.jsp >请重试<a>");
}
%>
</h1>
</body>
</html>
  相关解决方案