zhouyuchen
2009-03-26 14:19 阅读 252
已采纳

struts1 fileUpload得不到文件

我现在用的struts1框架,然后我做一个上传,用commons.fileupload做.

jsp文件:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">








我在Action中 的代码如下
DiskFileItemFactory factory=new DiskFileItemFactory();
String path=request.getSession().getServletContext().getRealPath("/upload");
System.out.println(path+"000");
factory.setRepository(new File(path));
factory.setSizeThreshold(1024*1024);
ProjectAccountsUploadForm pau=(ProjectAccountsUploadForm)form;
System.out.println("----"+pau.getFileItem());
ServletFileUpload upload=new ServletFileUpload(factory);
try
{
List list=upload.parseRequest(request);
for(FileItem item:list)
{
if(item.isFormField())
{
String name=item.getFieldName();
System.out.println(name+"111");
String value=item.getString("UTF-8");
request.setAttribute(name, value);
}
else
{
String name=item.getFieldName();
System.out.println(name+"222");
String value=item.getName();
System.out.println(value+"222");
int start=value.lastIndexOf("\");
String fileName=value.substring(start+1);
System.out.println(fileName+"222");
//request.setAttribute(name, fileName);

                //item.write(new File(path,fileName));将档案写到上存目

                }
        }
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }

问1:就这么简单的一个FORM ,发过去就是list 是空的(list.isEmpty()=true) 这是为什么?
我的想法就是用struts的框架,但我不用他的标签,到action中后我用fileupload来上传,请指点!!
问2:有人说用struts的html:file,我在JSP中加入<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
然后用/html:file
这个页面一访问就报错,是什么原因!javax/xml/ws/WebServiceRef

com.caucho.jsp.JspParseException: javax/xml/ws/WebServiceRef
at com.caucho.jsp.JspCompilerInstance.compile(JspCompilerInstance.java:440)
at com.caucho.jsp.JspManager.compile(JspManager.java:242)
at com.caucho.jsp.JspManager.createPage(JspManager.java:171)
at com.caucho.jsp.JspManager.createPage(JspManager.java:150)
at com.caucho.jsp.PageManager.getPage(PageManager.java:248)
at com.caucho.jsp.PageManager.getPage(PageManager.java:178)
at com.caucho.jsp.PageManager.getPage(PageManager.java:161)
at com.caucho.jsp.QServlet.getSubPage(QServlet.java:295)
at com.caucho.jsp.QServlet.getPage(QServlet.java:210)
at com.caucho.server.dispatch.PageFilterChain.compilePage(PageFilterChain.java:238)
at com.caucho.server.dispatch.PageFilterChain.doFilter(PageFilterChain.java:145)
at fd.common.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:103)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:187)
at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:265)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:273)
at com.caucho.server.port.TcpConnection.run(TcpConnection.java:682)
at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:730)
at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:649)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.lang.NoClassDefFoundError: javax/xml/ws/WebServiceRef
at com.caucho.jsp.TagAnalyzer.analyzeByReflection(TagAnalyzer.java:278)
at com.caucho.jsp.TagAnalyzer.analyze(TagAnalyzer.java:101)
at com.caucho.jsp.TagAnalyzer.analyze(TagAnalyzer.java:92)
at com.caucho.jsp.TagAnalyzer.analyze(TagAnalyzer.java:92)
at com.caucho.jsp.TagAnalyzer.analyze(TagAnalyzer.java:92)
at com.caucho.jsp.TagAnalyzer.analyze(TagAnalyzer.java:92)
at com.caucho.jsp.TagAnalyzer.analyze(TagAnalyzer.java:92)
at com.caucho.jsp.TaglibManager.analyzeTag(TaglibManager.java:135)
at com.caucho.jsp.ParseTagManager.analyzeTag(ParseTagManager.java:72)
at com.caucho.jsp.TagInstance.(TagInstance.java:118)
at com.caucho.jsp.TagInstance.addTag(TagInstance.java:305)
at com.caucho.jsp.java.GenericTag.generatePrologue(GenericTag.java:346)
at com.caucho.jsp.java.JspContainerNode.generatePrologueChildren(JspContainerNode.java:466)
at com.caucho.jsp.java.JspNode.generatePrologue(JspNode.java:687)
at com.caucho.jsp.java.JavaJspGenerator.generatePageHeader(JavaJspGenerator.java:955)
at com.caucho.jsp.java.JavaJspGenerator.generate(JavaJspGenerator.java:733)
at com.caucho.jsp.java.JavaJspGenerator.generate(JavaJspGenerator.java:603)
at com.caucho.jsp.JspCompilerInstance.generate(JspCompilerInstance.java:465)
at com.caucho.jsp.JspCompilerInstance.compile(JspCompilerInstance.java:386)
at com.caucho.jsp.JspManager.compile(JspManager.java:242)
at com.caucho.jsp.JspManager.createPage(JspManager.java:171)
at com.caucho.jsp.JspManager.createPage(JspManager.java:150)
at com.caucho.jsp.PageManager.getPage(PageManager.java:248)
at com.caucho.jsp.PageManager.getPage(PageManager.java:178)
at com.caucho.jsp.PageManager.getPage(PageManager.java:161)
at com.caucho.jsp.QServlet.getSubPage(QServlet.java:295)
at com.caucho.jsp.QServlet.getPage(QServlet.java:210)
at com.caucho.server.dispatch.PageFilterChain.compilePage(PageFilterChain.java:238)
at com.caucho.server.dispatch.PageFilterChain.doFilter(PageFilterChain.java:145)
at fd.common.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:103)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:187)
at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:265)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:273)
at com.caucho.server.port.TcpConnection.run(TcpConnection.java:682)
at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:730)
at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:649)
at java.lang.Thread.run(Thread.java:595)

Caused by: java.lang.NoClassDefFoundError: javax/xml/ws/WebServiceRef 应该是这个问题 ,但是我把其相应的jar加到工程里了(jaxws-api.jar),还是报这个错误.
[b]问题补充:[/b]
我现在的模式是用action,然后用的spring,将action交给了spring
http://blog.csdn.net/nalis/archive/2007/05/02/1594756.aspx
这里说不能配置actionForm,我把actionForm去掉了,还是不行,FileItem还是没有,我在想是不是我用了spring代理的问题呢 ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

7条回答 默认 最新

  • 已采纳
    leonelwong 马勒格彼得 2009-03-27 09:33

    最好是加个NAME属性,在解析的时候是根据NAME进行赋值的,万一不行就配置FormFile
    这种方式进行上传应该没什么问题,唯一的缺点就是如果多上传需要配置多个FormFile
    你可以去搜索一下,具体的使用方法,很简单

    点赞 评论 复制链接分享
  • wanghaolovezlq wanghaolovezlq 2009-03-26 14:33

    没了这个类
    java.lang.NoClassDefFoundError: javax/xml/ws/WebServiceRef
    你确认下你的jar是否少了

    点赞 评论 复制链接分享
  • zhoujuan520 zhoujuan520 2009-03-26 14:39

    [code="java"]
    //我的一个项目里面的代码给你贴出来
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List items = (List) upload.parseRequest(request); // 获得上传文件列表
    Iterator iter = items.iterator();
    while (iter.hasNext()) {
    FileItem item = (FileItem) iter.next();
    File file = new File("保存的到的路径");
    item.write(file); // 开始上传写入服务器
    }
    [/code]

    Action 配置
    [code="xml"]

    [/code]

    页面

    点赞 评论 复制链接分享
  • li28544071 li28544071 2009-03-26 16:03

    给你加个name属性

    点赞 评论 复制链接分享
  • iteye_15036 iteye_15036 2009-03-26 17:58

    点赞 评论 复制链接分享
  • jxdchina Shadj 2009-03-26 18:03

    lt0604 说的正是。把你代码调了一下,没加name属性跑不通,加了后正常了。
    [code="java"][/code]

    点赞 评论 复制链接分享
  • li28544071 li28544071 2009-03-27 10:25

    3.15刚过,居然还存在做事不给银子的事情,我心哇凉哇凉地

    点赞 评论 复制链接分享

相关推荐