Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 793|回复: 0

≈strust2 文件上传问题 ◇

[复制链接]

该用户从未签到

发表于 2011-10-31 13:42:24 | 显示全部楼层 |阅读模式
yi开始是做 获取表单qingqiuDe试1验 但shi我发现我一用struts,du取的内rong就讀不出來,Ru果仅仅是request.getParameter("XX")还可以读取到can数,但是getInputStream()就讀不出laili面De信息,页面上显示的也shi白板,部署时没問题,也沒抛异chang。dan是如果不用struts的话,就一切正常。yi开始我還以为是沒导那些上传文jian用的包的原因,后來发现其实是有的。请问各位大daDao底什么原yin?
欢迎来到Java学习者lun坛,zhuan载请注ming地址:http://www.javaxxz.com.
回复

使用道具 举报

该用户从未签到

发表于 2011-10-31 13:42:34 | 显示全部楼层

Re:≈strust2

好久沒有 sf
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-10-31 13:42:42 | 显示全部楼层

Re:≈strust2

用getInputStream()之前,试下用clear()清空下流
回复 支持 反对

使用道具 举报

该用户从未签到

 楼主| 发表于 2011-10-31 13:42:49 | 显示全部楼层

Re:≈strust2

form action="" name="" 提交表单加了上传文件的属性了嘛?
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-10-31 13:42:56 | 显示全部楼层

Re:≈strust2

再者就是看下struts2 里的配置文件 inputstream 跟的参数是什么
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-10-31 13:43:06 | 显示全部楼层

Re:≈strust2

一开始InputStream是通过request.getInputStream()得到的,那么要clear()是new 一個InputStream出來吗?还是其他的什么?
不過我觉得这应该不是重点,因为在不用struts的时候,是可以很好的使用的。。。

之后我在网上查找解决方案时,看到了一个帖子,说是不用getInputStream()方法,用其他的方法從而达到上传的目的。然后我发现他提供的方法俺这里也不好使,他的方法是:

commonUpload.jsp
HTML code
<%@ page language="java" import="java.util.*,org.apache.commons.fileupload.disk.*,java.io.*,
org.apache.commons.fileupload.servlet.*,org.apache.commons.fileupload.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%
DiskFileItemFactory factory=new DiskFileItemFactory();
//设置上傳工厂的限制
factory.setSizeThreshold(1024*1024*20);
factory.setRepository(new File(request.getRealPath("/")));
//创建一个上传文件的ServletFileUpload对象
ServletFileUpload upload=new ServletFileUpload(factory);
//设置上传文件最大接收20M
upload.setSizeMax(20*1024*1024);
//處理http请求,items是所有的表单项
List items=upload.parseRequest(request);
//遍历所有的表单项
for(Iterator it=items.iterator();it.hasNext();){
    FileItem item=(FileItem)it.next();
    if(item.isFormField()){
        String name=item.getFieldName();
        String value=item.getString("GBK");
        out.println("表單域的name=value对为:"+name+"="+value+"<br/>");
    }else{
        //取得文件域的表单名
        String fieldName=item.getFieldName();
        //取得文件名
        String fileName=item.getName();
        //取得文件的类型
        String cotentType=item.getContentType();
        //以当前时间来生成文件的文件名
        FileOutputStream fos=new FileOutputStream(request.getRealPath("/")+System.currentTimeMillis()
            +fileName.substring(fileName.lastIndexOf("."),fileName.length()));
   
        //如果上传文件域对应文件的内容已经在内存中
        if(item.isInMemory()){
            fos.write(item.get());
        }else{   
        //如果文件内容不存在在内存中
            //获取上传文件的输入流
            InputStream is=item.getInputStream();
            byte[] buffer=new byte[1024];
            int len;
            //读取上传文件的內容,并将其写入服务器的文件中
            while((len=is.read(buffer))>0){
                fos.write(buffer,0,len);
            }
            is.close();
            fos.close();
        }
    }
}


%>





用了他的方法最后输出依旧是没有完成上传效果,然后我在
List items=upload.parseRequest(request);
代码后加上了一句out.println(items);
顯示的结果是[]
也就是说在表单提交到页面的过程中还是什么都没取到

我的表单页面application.jsp代码是:
HTML code
<%@ page language="java" import="java.util.*" contentType="text/html;charset=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">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'application.jsp' starting page</title>
   
   

  </head>
  
  <body>
    <form id="form1" name="form1" enctype="multipart/form-data" method="post" action="Login">
    上传文件:<input type="file" name="file"/><br/>
  请求參数:<input type="text" name="wawa"/><br/>
  <input name="dd" type="submit" value="提交"/>
    </form>
  </body>
</html>



然后struts.xml
XML code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>
<constant name="devMode" value="true"></constant>
<package name="lee" namespace="/" extends="struts-default">
<action name="Login" class="lee.LoginAction">
<result name="success">/commonUpload.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>



然后是LoginAction.java
Java code
package lee;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

    @Override
    public String execute() throws Exception{
        return SUCCESS;
    }



最后是使用時的log:
2011-10-28 16:07:56,091 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:68 - Entering nullPropertyValue [target=[com.opensymphony.xwork2.DefaultTextProvider@cb9c42], property=struts]
2011-10-28 16:07:56,092 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:68 - Entering nullPropertyValue [target=[com.opensymphony.xwork2.DefaultTextProvider@cb9c42], property=org]
2011-10-28 16:07:59,161 INFO org.apache.struts2.dispatcher.Dispatcher.info:42 - Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir
2011-10-28 16:07:59,162 DEBUG org.apache.struts2.dispatcher.Dispatcher.debug:68 - saveDir=D:\\JavaUseTools\\apache-tomcat-7.0.11\\apache-tomcat-7.0.11\\work\\Catalina\\localhost\\struts2_test_0800
2011-10-28 16:07:59,271 DEBUG org.apache.struts2.dispatcher.multipart.MultiPartRequest.debug:68 - Found item file
2011-10-28 16:07:59,271 DEBUG org.apache.struts2.dispatcher.multipart.MultiPartRequest.debug:68 - Item is a file upload
2011-10-28 16:07:59,272 DEBUG org.apache.struts2.dispatcher.multipart.MultiPartRequest.debug:68 - Found item wawa
2011-10-28 16:07:59,273 DEBUG org.apache.struts2.dispatcher.multipart.MultiPartRequest.debug:68 - Item is a normal form field
2011-10-28 16:07:59,273 DEBUG org.apache.struts2.dispatcher.multipart.MultiPartRequest.debug:68 - Found item dd
2011-10-28 16:07:59,274 DEBUG org.apache.struts2.dispatcher.multipart.MultiPartRequest.debug:68 - Item is a normal form field
2011-10-28 16:07:59,275 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:68 - Entering nullPropertyValue [target=[com.opensymphony.xwork2.DefaultTextProvider@cb9c42], property=struts]
2011-10-28 16:07:59,276 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:68 - Entering nullPropertyValue [target=[com.opensymphony.xwork2.DefaultTextProvider@cb9c42], property=struts]
2011-10-28 16:07:59,276 DEBUG com.opensymphony.xwork2.DefaultActionProxy.debug:68 - Creating an DefaultActionProxy for namespace / and action name Login
2011-10-28 16:07:59,277 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:68 - intercept '//Login' {  
2011-10-28 16:07:59,279 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:68 - applied invocation context locale=en
2011-10-28 16:07:59,280 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:68 - before Locale=en
2011-10-28 16:07:59,280 DEBUG com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.debug:68 - Setting static parameters {}
2011-10-28 16:07:59,281 DEBUG com.opensymphony.xwork2.interceptor.ParametersInterceptor.debug:68 - Setting params NONE
2011-10-28 16:07:59,281 DEBUG com.opensymphony.xwork2.interceptor.ParametersInterceptor.debug:68 - Setting params dd => [ 提交 ] file => [ D:\\JavaUseTools\\apache-tomcat-7.0.11\\apache-tomcat-7.0.11\\work\\Catalina\\localhost\\struts2_test_0800\\upload__7f40b9a_1334992381b__8000_00000000.tmp ] fileContentType => [ text/plain ] fileFileName => [ eclipse快捷鍵.txt ] wawa => [ ]  
2011-10-28 16:07:59,288 DEBUG org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.debug:68 - Validating //Login with method execute.
2011-10-28 16:07:59,289 DEBUG com.opensymphony.xwork2.validator.ValidationInterceptor.debug:68 - Invoking validate() on action lee.LoginAction@ebf121
2011-10-28 16:07:59,289 DEBUG com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil.debug:68 - cannot find method [validateExecute] in action [lee.LoginAction@ebf121]
2011-10-28 16:07:59,290 DEBUG com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil.debug:68 - cannot find method [validateDoExecute] in action [lee.LoginAction@ebf121]
2011-10-28 16:07:59,291 DEBUG com.opensymphony.xwork2.DefaultActionInvocation.debug:68 - Executing action method = null
2011-10-28 16:07:59,292 DEBUG org.apache.struts2.dispatcher.ServletDispatcherResult.debug:68 - Forwarding to location /commonUpload.jsp
2011-10-28 16:07:59,292 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:68 - Entering nullPropertyValue [target=[lee.LoginAction@ebf121, com.opensymphony.xwork2.DefaultTextProvider@cb9c42], property=struts]
2011-10-28 16:07:59,298 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:68 - Entering nullPropertyValue [target=[lee.LoginAction@ebf121, com.opensymphony.xwork2.DefaultTextProvider@cb9c42], property=org]
2011-10-28 16:07:59,301 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:68 - after Locale=en
2011-10-28 16:07:59,302 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:68 - intercept }

那位大哥 行行好 帮俺分析一下 ~~~~~~~~實在是郁悶了很久了
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-10-31 13:43:14 | 显示全部楼层

Re:≈strust2

我的enctype属性设置的是multipart/form-data
就算enctype属性用的是默认值,也是应该能够取到文件域file,文本域wawa,提交按钮bb,这三个參数才對


然后你说的那个去配置文件里找InputStream跟的参数 应该去哪里找呢?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-5-3 11:20 , Processed in 0.412411 second(s), 46 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表