随笔-34  评论-1965  文章-0  trackbacks-0

前一阵子有些朋友在电子邮件中问关于Struts 2实现文件上传的问题, 所以今天我们就来讨论一下这个问题。

实现原理

Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的文件。

具体实现

前段时间Apache发布了Struts 2.0.6 GA,所以本文的实现是以该版本的Struts作为框架的。以下是例子所依赖类包的列表:

依赖类包的列表 
清单1 依赖类包的列表

首先,创建文件上传页面FileUpload.jsp,内容如下:

<% @ page language = " java " contentType = " text/html; charset=utf-8 " pageEncoding = " utf-8 " %>
<% @ taglib prefix = " s " uri = " /struts-tags " %>

<! 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 >
   
< title > Struts 2 File Upload </ title >
</ head >
< body >
   
< s:form action ="fileUpload" method ="POST" enctype ="multipart/form-data" >
       
< s:file name ="myFile" label ="Image File" />
       
< s:textfield name ="caption" label ="Caption" />        
       
< s:submit />
   
</ s:form >
</ body >
</ html >
清单2 FileUpload.jsp

在FileUpload.jsp中,先将表单的提交方式设为POST,然后将enctype设为multipart/form-data,这并没有什么特别之处。接下来,<s:file/>标志将文件上传控件绑定到Action的myFile属性。

其次是FileUploadAction.java代码:

package tutorial;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {
   
private static final long serialVersionUID = 572146812454l ;
   
private static final int BUFFER_SIZE = 16 * 1024 ;
   
   
private File myFile;
   
private String contentType;
   
private String fileName;
   
private String imageFileName;
   
private String caption;
   
   
public void setMyFileContentType(String contentType) {
       
this .contentType = contentType;
   }

   
   
public void setMyFileFileName(String fileName) {
       
this .fileName = fileName;
   }

       
   
public void setMyFile(File myFile) {
       
this .myFile = myFile;
   }

   
   
public String getImageFileName() {
       
return imageFileName;
   }

   
   
public String getCaption() {
       
return caption;
   }


   
public void setCaption(String caption) {
       
this .caption = caption;
   }

   
   
private static void copy(File src, File dst) {
       
try {
           InputStream in
= null ;
           OutputStream out
= null ;
           
try {                
               in
= new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
               out
= new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
               
byte [] buffer = new byte [BUFFER_SIZE];
               
while (in.read(buffer) > 0 ) {
                   out.write(buffer);
               }

           }
finally {
               
if ( null != in) {
                   in.close();
               }

               
if ( null != out) {
                   out.close();
               }

           }

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

   }

   
   
private static String getExtention(String fileName) {
       
int pos = fileName.lastIndexOf( " . " );
       
return fileName.substring(pos);
   }


   @Override
   
public String execute()     {        
       imageFileName
= new Date().getTime() + getExtention(fileName);
       File imageFile
= new File(ServletActionContext.getServletContext().getRealPath( " /UploadImages " ) + " / " + imageFileName);
       copy(myFile, imageFile);
       
return SUCCESS;
   }

   
}
清单3 tutorial/FileUploadAction.java

在FileUploadAction中我分别写了setMyFileContentType、setMyFileFileName、setMyFile和setCaption四个Setter方法,后两者很容易明白,分别对应FileUpload.jsp中的<s:file/>和<s:textfield/>标志。但是前两者并没有显式地与任何的页面标志绑定,那么它们的值又是从何而来的呢?其实,<s:file/>标志不仅仅是绑定到myFile,还有myFileContentType(上传文件的MIME类型)和myFileFileName(上传文件的文件名,该文件名不包括文件的路径)。因此,<s:file name="xxx" />对应Action类里面的xxx、xxxContentType和xxxFileName三个属性。

FileUploadAction作用是将浏览器上传的文件拷贝到WEB应用程序的UploadImages文件夹下,新文件的名称是由系统时间与上传文件的后缀组成,该名称将被赋给imageFileName属性,以便上传成功的跳转页面使用。

下面我们就来看看上传成功的页面:

<% @ page language = " java " contentType = " text/html; charset=utf-8 " pageEncoding = " utf-8 " %>
<% @ taglib prefix = " s " uri = " /struts-tags " %>

<! 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 >
   
< title > Struts 2 File Upload </ title >
</ head >
< body >
   
< div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" >
       
< img src ='UploadImages/<s:property value ="imageFileName" /> ' />
       
< br />
       
< s:property value ="caption" />
   
</ div >
</ body >
</ html >
清单4 ShowUpload.jsp

ShowUpload.jsp获得imageFileName,将其UploadImages组成URL,从而将上传的图像显示出来。

然后是Action的配置文件:

<? xml version="1.0" encoding="UTF-8" ?>

<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>

< struts >
   
< package name ="fileUploadDemo" extends ="struts-default" >
       
< action name ="fileUpload" class ="tutorial.FileUploadAction" >
           
< interceptor-ref name ="fileUploadStack" />
           
< result name ="success" > /ShowUpload.jsp </ result >
       
</ action >
   
</ package >
</ struts >
清单5 struts.xml

fileUpload Action显式地应用fileUploadStack的拦截器。

最后是web.xml配置文件:

<? xml version="1.0" encoding="UTF-8" ?>
< web-app id ="WebApp_9" version ="2.4"
    xmlns
="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

   
< display-name > Struts 2 Fileupload </ display-name >

   
< filter >
       
< filter-name > struts-cleanup </ filter-name >
       
< filter-class >
            org.apache.struts2.dispatcher.ActionContextCleanUp
       
</ filter-class >
   
</ filter >
    
   
< filter >
       
< filter-name > struts2 </ filter-name >
       
< filter-class >
            org.apache.struts2.dispatcher.FilterDispatcher
       
</ filter-class >
   
</ filter >
    
   
< filter-mapping >
       
< filter-name > struts-cleanup </ filter-name >
       
< url-pattern > /* </ url-pattern >
   
</ filter-mapping >

   
< filter-mapping >
       
< filter-name > struts2 </ filter-name >
       
< url-pattern > /* </ url-pattern >
   
</ filter-mapping >

   
< welcome-file-list >
       
< welcome-file > index.html </ welcome-file >
   
</ welcome-file-list >

</ web-app >
清单6 WEB-INF/web.xml

发布运行应用程序,在浏览器地址栏中键入:http://localhost:8080/Struts2_Fileupload/FileUpload.jsp,出现图示页面:


清单7 FileUpload页面

选择图片文件,填写Caption并按下Submit按钮提交,出现图示页面:


清单8 上传成功页面

更多配置

在运行上述例子,如果您留心一点的话,应该会发现服务器控制台有如下输出:

Mar 20 , 2007 4 : 08 : 43 PM org.apache.struts2.dispatcher.Dispatcher getSaveDir
INFO: Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir
Mar
20 , 2007 4 : 08 : 43 PM org.apache.struts2.interceptor.FileUploadInterceptor intercept
INFO: Removing file myFile C:\Program Files\Tomcat
5.5 \work\Catalina\localhost\Struts2_Fileupload\upload_251447c2_1116e355841__7ff7_00000006.tmp
清单9 服务器控制台输出

上述信息告诉我们,struts.multipart.saveDir没有配置。struts.multipart.saveDir用于指定存放临时文件的文件夹,该配置写在struts.properties文件中。例如,如果在struts.properties文件加入如下代码:

struts.multipart.saveDir = /tmp
清单10 struts配置

这样上传的文件就会临时保存到你根目录下的tmp文件夹中(一般为c:\tmp),如果此文件夹不存在,Struts 2会自动创建一个。

错误处理

上述例子实现的图片上传的功能,所以应该阻止用户上传非图片类型的文件。在Struts 2中如何实现这点呢?其实这也很简单,对上述例子作如下修改即可。

首先修改FileUpload.jsp,在<body>与<s:form>之间加入“<s:fielderror />”,用于在页面上输出错误信息。

然后修改struts.xml文件,将Action fileUpload的定义改为如下所示:

        < action name ="fileUpload" class ="tutorial.FileUploadAction" >
           
< interceptor-ref name ="fileUpload" >
               
< param name ="allowedTypes" >
                    image/bmp,image/png,image/gif,image/jpeg
               
</ param >
           
</ interceptor-ref >
           
< interceptor-ref name ="defaultStack" />            
           
< result name ="input" > /FileUpload.jsp </ result >
           
< result name ="success" > /ShowUpload.jsp </ result >
       
</ action >
清单11 修改后的配置文件

显而易见,起作用就是fileUpload拦截器的allowTypes参数。另外,配置还引入defaultStack它会帮我们添加验证等功能,所以在出错之后会跳转到名称为“input”的结果,也即是FileUpload.jsp。

发布运行应用程序,出错时,页面如下图所示:


清单12 出错提示页面

上面的出错提示是Struts 2默认的,大多数情况下,我们都需要自定义和国际化这些信息。通过在全局的国际资源文件中加入“struts.messages.error.content.type.not.allowed=The file you uploaded is not a image”,可以实现以上提及的需求。对此有疑问的朋友可以参考我之前的文章《在Struts 2.0中国际化(i18n)您的应用程序》。

实现之后的出错页面如下图所示:


清单13 自定义出错提示页面

同样的做法,你可以使用参数“maximumSize”来限制上传文件的大小,它对应的字符资源名为:“struts.messages.error.file.too.large”。

字符资源“struts.messages.error.uploading”用提示一般的上传出错信息。

多文件上传

与单文件上传相似,Struts 2实现多文件上传也很简单。你可以将多个<s:file />绑定Action的数组或列表。如下例所示。

< s:form action ="doMultipleUploadUsingList" method ="POST" enctype ="multipart/form-data" >
   
< s:file label ="File (1)" name ="upload" />
   
< s:file label ="File (2)" name ="upload" />
   
< s:file label ="FIle (3)" name ="upload" />
   
< s:submit />
</ s:form >
清单14 多文件上传JSP代码片段

如果你希望绑定到数组,Action的代码应类似:

    private File[] uploads;
   
private String[] uploadFileNames;
   
private String[] uploadContentTypes;

   
public File[] getUpload() { return this .uploads; }
   
public void setUpload(File[] upload) { this .uploads = upload; }

   
public String[] getUploadFileName() { return this .uploadFileNames; }
   
public void setUploadFileName(String[] uploadFileName) { this .uploadFileNames = uploadFileName; }

   
public String[] getUploadContentType() { return this .uploadContentTypes; }
   
public void setUploadContentType(String[] uploadContentType) { this .uploadContentTypes = uploadContentType; }
清单15 多文件上传数组绑定Action代码片段

如果你想绑定到列表,则应类似:

    private List < File > uploads = new ArrayList < File > ();
   
private List < String > uploadFileNames = new ArrayList < String > ();
   
private List < String > uploadContentTypes = new ArrayList < String > ();

   
public List < File > getUpload() {
       
return this .uploads;
   }

   
public void setUpload(List < File > uploads) {
       
this .uploads = uploads;
   }


   
public List < String > getUploadFileName() {
       
return this .uploadFileNames;
   }

   
public void setUploadFileName(List < String > uploadFileNames) {
       
this .uploadFileNames = uploadFileNames;
   }


   
public List < String > getUploadContentType() {
       
return this .uploadContentTypes;
   }

   
public void setUploadContentType(List < String > contentTypes) {
       
this .uploadContentTypes = contentTypes;
   }
清单16 多文件上传列表绑定Action代码片段

总结

在Struts 2中实现文件上传的确是轻而易举,您要做的只是使用<s:file />与Action的属性绑定。这又一次有力地证明了Struts 2的简单易用。

posted on 2007-03-21 00:48 Max 阅读(108521) 评论(148)  编辑  收藏 所属分类: Struts 2.0系列
评论共2页: 1 2 下一页 

评论:
# re: 在Struts 2中实现文件上传 2007-08-29 13:50 | Ouch
好像还应该加入包servlet-api.jar !!!  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-09-03 16:58 | li
好像filename取出来的是null呀,有人碰到这种情况吗?  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2007-09-04 09:40 | Mike
我取出来的filename也是null啊,到底怎么搞的,有些地方没有讲清楚  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-09-04 16:13 | tf
我取出的也是null,
运行出现下面的错误提示,也就是值都没有传过去.请高手指教
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
tutorial.FileUpload.getExtention(FileUpload.java:76)
tutorial.FileUpload.execute(FileUpload.java:82)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:334)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:195)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.intercept(ParametersInterceptor.java:118)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.intercept(PrepareInterceptor.java:115)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:155)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:180)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:204)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
org.apache.struts2.impl.StrutsActionProxy$1.call(StrutsActionProxy.java:25)
org.apache.struts2.impl.StrutsActionProxy$1.call(StrutsActionProxy.java:24)
org.apache.struts2.impl.RequestContextImpl.callInContext(RequestContextImpl.java:147)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:23)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:317)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:242)

  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-09-04 17:03 | tf
怎么没有人回答?请高手指教  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-09-05 00:35 | Max
@Mike
@tf
请细心对照我文中的步骤去做,结果应该会出来的。
或者你的WEB-INF/web.xml的内容,是否有加入:
< filter >
< filter-name > struts2 </ filter-name >
< filter-class >
org.apache.struts2.dispatcher.FilterDispatcher
</ filter-class >
</ filter >  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2007-09-07 15:59 | Mike
@li
@tf
我想我大概知道错误的原因了,你们可能是漏了一些get/set方法,或者是自己在struts.xml中加入了<interceptor name ="fileUploadStack" class ="tutorial.FileUploadInterceptor" />,这个不需要自己加入的,是struts2内置的interceptor,会自动调用。我希望我的解决方法可以对你们有用,这里也要对max说声对不起了,没有跑出来是自己没按照规则来做。  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-09-10 16:59 | tf
请问Max可以出个和文件上传对应的文件下载的例子吗?  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-09-10 17:09 | tf
谁有和文件下载的的例子的麻烦发到我邮箱好吗?
我的邮箱是susu_zi@126.com  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-09-12 15:53 | shenchong
Max,请问一下为什么我在做多附件上传的时候uploads里面的内容是String型的,而不是File类型的  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-09-13 00:19 | Max
@tf
Struts 2 的Show Case中有相关的例子
@shenchong
你看错了吧?  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-09-13 11:04 | laibin
学习中!
在此感谢Max!  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-09-13 17:46 | shenchong
@Max
我没有看错的,我试过了show case中的相关例子,它拿到的的确是List类型的,但我拿到的却是String的,是不是还需要在哪里配置?或者是jar包的版本问题?  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-09-14 09:53 | ILOVEYOU

@Max
我现在出现如下异常,图片显示不出来

java.io.FileNotFoundException: D:\Java\jetty-6.1.3\webapps\uploadtest\UploadImages\1189733933484.bmp (系统找不到指定的路径。)
 at java.io.FileOutputStream.open(Native Method)
 at java.io.FileOutputStream.
<init>(FileOutputStream.java:179)
 at java.io.FileOutputStream.
<init>(FileOutputStream.java:131)
 at tutorial.FileUploadAction.copy(FileUploadAction.java:
87)
 at tutorial.FileUploadAction.execute(FileUploadAction.java:
114)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
 at java.lang.reflect.Method.invoke(Method.java:
585)
 at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:
404)
 at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:
267)
 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
229)
 at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:
123)
 at com.opensymphony.xwork2.DefaultActionInvocation$
2.doProfiling(DefaultActionInvocation.java:224)
 at com.opensymphony.xwork2.DefaultActionInvocation$
2.doProfiling(DefaultActionInvocation.java:223)
 at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:
455)
 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
221)
 at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:
167)
 at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:
86)
 at com.opensymphony.xwork2.DefaultActionInvocation$
2.doProfiling(DefaultActionInvocation.java:224)
 at com.opensymphony.xwork2.DefaultActionInvocation$
2.doProfiling(DefaultActionInvocation.java:223)
 at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:
455)
 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
221)
 at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:
83)
 at com.opensymphony.xwork2.DefaultActionInvocation$
2.doProfiling(DefaultActionInvocation.java:224)
 at com.opensymphony.xwork2.DefaultActionInvocation$
2.doProfiling(DefaultActionInvocation.java:223)
 at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:
455)
 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
221)
 at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:
121)
 at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:
86)
 at com.opensymphony.xwork2.DefaultActionInvocation$
2.doProfiling(DefaultActionInvocation.java:224)
 at com.opensymphony.xwork2.DefaultActionInvocation$
2.doProfiling(DefaultActionInvocation.java:223)
 at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:
455)
 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
221)
 at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:
170)
 at com.opensymphony.xwork2.DefaultActionInvocation$
2.doProfiling(DefaultActionInvocation.java:224)
 at com.opensymphony.xwork2.DefaultActionInvocation$
2.doProfiling(DefaultActionInvocation.java:223)
 at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:
455)
 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
221)
 at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:
176)
 at com.opensymphony.xwork2.DefaultActionInvocation$
2.doProfiling(DefaultActionInvocation.java:224)
 at com.opensymphony.xwork2.DefaultActionInvocation$
2.doProfiling(DefaultActionInvocation.java:223)
 at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:
455)
 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
221)
 at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:
268)
 at com.opensymphony.xwork2.DefaultActionInvocation$
2.doProfiling(DefaultActionInvocation.java:224)
 at com.opensymphony.xwork2.DefaultActionInvocation$
2.doProfiling(DefaultActionInvocation.java:223)
 at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:
455)
 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
221)
 at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:
50)
 at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:
504)
 at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:
419)
 at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:
1089)
 at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:
99)
 at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:
1089)
 at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:
365)
 at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:
216)
 at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:
181)
 at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:
712)
 at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:
405)
 at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:
211)
 at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:
114)
 at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:
139)
 at org.mortbay.jetty.Server.handle(Server.java:
285)
 at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:
502)
 at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:
835)
 at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:
641)
 at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:
208)
 at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:
378)
 at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:
368)
 at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:
442)
2007-9-14 9:38:54 org.apache.struts2.interceptor.FileUploadInterceptor intercept
信息: Removing file myFile \tmp\upload_4b5952e6_11501ab2110__8000_00000000.tmp


大家帮我看看

  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-09-14 22:34 | leson
提醒一下,MAX示例代码中拷贝文件部分:
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}

有点问题,如果文件的大小,%BUFFERSIZE的余数不为0,则会造成多拷贝了N多K字节,又加之buffer[]里面剩余部分没有被覆盖,导致最后的文件和原来的文件前面部分相等,后面的部分就没有多少规律了,但却又不是全空。

修正方法是:
byte[] buffer = new byte[BUFFER_SIZE];
for (int byteRead = 0; (byteRead = in.read(buffer)) > 0; )
{
out.write(buffer, 0, byteRead);
}
最后一次写入该写的字节就可以了。
  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2007-09-26 22:14 | Jimmy
文件类型里明明有jpeg,png的,为什么不能上传?
Content-Type not allowed: myFile "upload_3eb7e010_115421d641d__8000_00000032.tmp" image/pjpeg

Content-Type not allowed: myFile "upload_3eb7e010_115421d641d__8000_00000034.tmp" image/x-png  回复  更多评论
  
# 遇到了同样的问题 2007-10-09 12:00 | referee
上面有人出现的问题我也遇到了,搞不来,哪位高人帮帮忙
java.io.FileNotFoundException: F:\cvs_root\struts\WebContent\UploadImages\1191902350707.bmp (系统找不到指定的路径。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at tutorial.FileUploadAction.copy(FileUploadAction.java:55)
at tutorial.FileUploadAction.execute(FileUploadAction.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:334)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:195)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.intercept(ParametersInterceptor.java:118)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.intercept(PrepareInterceptor.java:115)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:155)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:180)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:266)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
at org.apache.struts2.impl.StrutsActionProxy$1.call(StrutsActionProxy.java:25)
at org.apache.struts2.impl.StrutsActionProxy$1.call(StrutsActionProxy.java:24)
at org.apache.struts2.impl.RequestContextImpl.callInContext(RequestContextImpl.java:147)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:23)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:317)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:242)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Unknown Source)
2007-10-9 11:59:10 org.apache.struts2.interceptor.FileUploadInterceptor intercept
信息: Removing file myFile \tmp\upload__7d878dc9_11582e8d4f6__8000_00000002.tmp  回复  更多评论
  
# 知道答案了 2007-10-09 12:43 | referee
是文件夹要手动建立  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2007-10-10 12:42 | hehe
不错,什么时候来个struts2+hibernate+spring整合,期待中。。。。。。。。  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-10-31 16:57 | 赫连紫軒(puras)
Struts2.0的上传文件大小默认是2M
可以通过在struts.xml里来修改这个大小
<constant name="struts.multipart.maxSize" value="5242880" />

但是遇到个问题就是
这里假如我设置的是5M
而我的Action实上设置的为3M
此时我上传4M的文件的时候
可以捕捉到异常提示我文件过大
返回到上传页面
如果此时我上传的文件超过了5M
则会直接告诉我文件过大
虽然也会返回到上传页面
但之前上传页面的数据将会丢失
比如说从别的页面传递过来的一些参数.

请教下该如何解决?
谢谢.  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-11-01 10:27 | 游客
max你好,我一直关注着你的struts2,从开始接触struts2就是看的你的博客,受益匪浅,非常感谢你的介绍,现在网上关于struts的内容太少了。你说的批量上传文件,他的文件名里有系统时间,那同一次上传的文件,系统时间是一样的吗  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-11-23 11:07 | kayata
@游客
遇到一个问题,struts2和spring2集成后,在文件上传时,发现fileload拦截器不工作,设置的文件类型和大小都不起作用,不知为什么?,急  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-11-26 09:35 | @游客
点击文件上传时提示Invalid field value for field "myFile",你的myFile字段时File型,我把他改成String型就可以,然后做相应的修改,请问这到底时为何  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-11-29 09:16 | bolly
我在上传的action中加了拦截器以后,别的action请求也被拦截下来了,fileUpload是全局的吗?怎么解决?  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-11-29 09:37 | bolly
<action name="dofileUpload" class="fileUploadAction"
method="dofileUpload">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
application/vnd.ms-excel,text/plain
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="input">WEB-INF/jsp/zzz/upload.jsp</result>
<result>WEB-INF/jsp/zzz/upload.jsp</result>
</action>
<action name="uploadClear" class="fileUploadAction"
method="uploadClear">
<result name="input">WEB-INF/jsp/zzz/upload.jsp</result>
<result>WEB-INF/jsp/zzz/upload.jsp</result>
</action>
uploadClear提交不到action中去,怎么会这样?  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-11-29 10:15 | zoninge
大家帮我看看这个错误啊,包已经添进去了,怎么回事呢?

HTTP Status 404 - /photo/fileUpload

--------------------------------------------------------------------------------

type Status report

message /photo/fileUpload

description The requested resource (/photo/fileUpload) is not available.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.15  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-11-29 17:38 | zoninge
终于出来了
相当感谢啊
我是小菜鸟 多多指教!  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-12-22 09:59 | xtmtd
上传一个大文件时,STRUTS2的拦截器,会报错,说文件过大。但是一次上传几个大文件时,STRUTS2的拦截器就不报任何错误了,也没有提示,不知道为什么啊?  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2007-12-26 10:14 | suyuan
啊 我也出现了这个问题

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.RuntimeException: Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta) - [unknown location]
com.opensymphony.xwork2.inject.ContainerBuilder$4.create(ContainerBuilder.java:136)
com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:476)
com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:486)
com.opensymphony.xwork2.inject.ContainerImpl$9.call(ContainerImpl.java:517)
com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:542)
com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:515)
org.apache.struts2.dispatcher.Dispatcher.wrapRequest(Dispatcher.java:697)
org.apache.struts2.dispatcher.FilterDispatcher.prepareDispatcherAndWrapRequest(FilterDispatcher.java:330)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:390)
org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.23 logs.

filter 也配了 上传出现这个,。。。。  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2007-12-29 10:20 | wendy
这么写会存在并发问题吧?  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2007-12-29 21:56 | wendy
当我上传一个空文件时(比如说我建一个 新建文件.txt),报空指针异常,我觉得只要文件存在不管里面有没有内容都不应该报错的啊,为什么?  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-01-04 18:03 | ElgooG
写的东西不完全对啊  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-01-09 20:15 | szz
我想问一下,我的form中加了enctype="multipart/form-data",配了struts2的校验器校验其他的文本框就不起作用了,去了enctype="multipart/form-data",校验器就好用了,这是怎么回事呀?  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-01-22 01:01 | 飞飞
当我配置web.xml后,网站目录不能浏览,出现404错误!
请问这是什么问题?
谢谢.  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-02-20 16:11 | walkerstar
Struts2的文件上传只能说是简单的文件上传。
如果一个网站文件上传很重要的话,需要仔细的考虑各种可能性。
  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-02-22 13:10 | look
我无法通过拦截器控制上传类型,代码在下面,有谁能帮我看看,我试了很久fileUploadStack这个拦截器就是不起作用,什么文件都能上传。
struts.xml:
<include file ="struts-default.xml" />
<action name="fileUpload" class="struts.FileUpLoad">
<interceptor-ref name ="fileUploadStack" >
<param name ="allowedTypes" >
image/bmp,image/png,image/gif,image/jpeg
</param >
</interceptor-ref >
<interceptor-ref name ="defaultStack" /> <result name ="input" >/upload.jsp</result >
<result name="success">/show.jsp</result>

upload.jsp:
<s:fielderror></s:fielderror>
<s:form action="fileUpload" method="post" enctype="multipart/form-data">
<s:file name="myFile" label="file"></s:file>
<s:textfield name="caption" label ="Caption"></s:textfield>
<s:submit></s:submit>
</s:form>
<hr>
  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-03-13 21:50 | kisssa
@babala

要传.jpg格式的图片,应该是image/pjpeg。
我也不知道为什么,但我按照错误提示,修改后就OK了,希望对你有用  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-04-03 09:29 | xiaohu
各位有调通多文件上传的吗?多文件上传时,如何把filename,contentType和页面的<s:file>绑定呢??  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-04-14 19:53 | 才情乱舞
如果要上传到数据库怎么修改Action和struts.xml配置文件(结合hibernate和spring)  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-04-25 10:21 | re
<param name ="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,text/xml,application/zip
</param>

这里这样写还是不可以传xml格式和zip格式的  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-05-12 21:48 | Hathor
@eddie
我也打印不出来
不知道如何修改阻止提交文件的非图片类型以及
显示我友好的错误信息  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-05-27 20:27 | cathy
请问如何更改上传的路径啊
我想要更改成指定路径啊
请多多指教啊  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-05-29 06:06 | 123
321564165  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-06-02 15:23 | loda
asdasdsadad  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-07-05 11:46 | 爱爱爱
啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-07-08 19:13 | 按时的
asd   回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-07-24 09:13 | 王仁
还可以  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-07-24 10:13 | 王仁
asdfsdfsdf  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-07-24 10:14 | 王仁
爱不性  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-07-31 15:59 | xiexie
有完整例子吗包括LIB。发一份不胜感激cheerzan@yahoo.com.cn  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2008-08-07 13:08 | matrix
@Max
我作如下设置:
struts.xml:
<param name="maximumSize">102400</param>

globalMessage_zh_CN.properties:
struts.messages.error.file.too.large=上传文件太大!

strusts.properties:
struts.multipart.maxSize=1048576

当上传的图片大小Size:102400<Size<1048576时,会提示“上传文件太大!”,而当Size>1048576时,页面并不提示,尽管后台打印出了文件太大的信息。我怎么让它在页面也表现出来呢???  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-08-18 11:38 | mzy
# re: 在Struts 2中实现文件上传 2007-11-26 09:35 | @游客
点击文件上传时提示Invalid field value for field "myFile",我的也出现了这个问题,但是我解决了,因为我使用的是 普通的FORM ,没有使用<s:form>标签,但我的FORM 中,没有添加 method='post' 属性,添加后,可以上传文件了。 希望对其它人有帮助。  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-11-03 21:45 | yihaijian
为什么我什么都是对的,就是图片不显示啊,郁闷,大家帮帮忙啊...  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-11-12 11:18 | 车水马龙
@yihaijian
文件路径错误了吧  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-11-19 10:46 | lrl
感觉跟你们相见恨晚
向你们看齐·  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-11-19 10:56 | lrl
学习struts中,每次程序错误,上网都可以找到高人的解答方法,有种相见恨晚感觉,努力向你们看齐  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2008-12-16 10:28 | icewind
请问,为什么我上传的struts.xml中设置的所有类型的图片类型文件,都说是
The file you uploaded is not a image啊,还有知道的人 ?????  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2009-01-09 18:08 | 瑞瑞
想不到吧这位同学,2009年的1月9号我也碰到了和你同样的问题,弄不出来,我是按照struts2中文帮助文档做的,什么都没错,就是不显示图片,郁闷。。。。  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2009-01-11 13:52 | reaijava
正在学习Struts 2 支持  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2009-01-19 19:07 | shuzigui
我运行你的第二个例子的时候,就是限制文件类型时,出错,我按照你的配置修改struts.xml,在上传非图片格式的文件时,仍然可以上传上去,这是怎么回事呀?  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2009-01-19 19:18 | shuzigui
@Joe
你的错误是在FileUploadAction.java中getExtention方法下的fileName.lastIndexOf( "." );引号中只有一个点,而不是有有空格加点的(" . ")这种,我今天碰到这种问题,我把它改过来就可以运行第一个例子了  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2009-02-05 16:39 | sheepzhao
@zoninge
你的问题怎么解决的阿?我也出现这个问题了  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2009-03-17 21:15 | 2少
File imageFile = new File(ServletActionContext.getServletContext().getRealPath( " /UploadImages " ) + " / " + imageFileName);

请问一下,这句代码是用来干什么的呢?


我在编译Action的时候,老是说 无法访问javax.servlet.ServletContext
然后说上面那一句代码有错误  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2009-04-27 18:10 | tufu
刚学struts2,搞此上传文件例子时有如下错误:
java.lang.RuntimeException: Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta) - [unknown location]
恳求高人指点!mrzhangtufu@126.com
  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2009-05-29 16:17 | coobee
@ILOVEYOU

I encountered the same problem, it's because that the directory "UpdateImages" doesn't exist. Please invoke

FileUtils.copyFile(myFile, imageFile);

to replace the original copy method.

or

Add the below code in copy method to create the parent directory when it does't exist.

if (dst.getParentFile() != null && dst.getParentFile().exists() == false) {
if (dst.getParentFile().mkdirs() == false) {
throw new IOException("Destination '" + dst + "' directory cannot be created");
}
}
  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2009-08-05 14:03 | 第三方
你能说一下怎么存到数据库么,发到736732716@qq.com 谢谢  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2009-08-09 12:53 | HAHA
太感谢 了。   回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2009-08-09 12:53 | HAHA
thanks   回复  更多评论
  
# re: 在Struts 2中实现文件上传 2009-10-12 22:18 | 哥们
在struts2上传时,我碰到个问题,想请教
如果某个文章记录已经上传了图片,而后来发现这个记录要修改,但不需要重新上传图片,只需要修改其它的字段,提交后,发现原来上传的图片记录就没有了

为此我还得在页面另外隐藏一个字段,并且在action里做大段处理代码。想请问一下你是否碰到过此问题
  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2009-11-04 22:38 | zx
存数据库里只需要把fileName图片名存进去i就OK,其实也没必要存数据库的图片的话,放服务器上就行  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2009-11-27 15:08 | zc
如果我多文件上传,一个文件只准上传图片格式,另外一个没有限制了,该怎么办?  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2009-12-10 14:38 | wo
請問下,我照你的全都寫了,在上傳那個頁面不管你上傳什麼都不會跳過去,根本一點跳動都沒有,不知道是什麼原因?有沒高手遇到過?  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2009-12-10 14:44 | wo
@@wo
我都是一字不漏的全復過來的,就是在上傳那個頁面根本就不跳,,發呆了  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2009-12-11 15:34 | wo
您好,我想请问下您写的第一个我试了下报出下面异常:
imageFileName===1260516248015.jpg
java.io.FileNotFoundException: D:\Tomcat 6.0\webapps\Struts28\UploadImages (存取被拒。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
at tutorial.FileUploadAction.copy(FileUploadAction.java:57)
at tutorial.FileUploadAction.execute(FileUploadAction.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:306)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
我在路径下找不到图片,感觉好像只是得到了图片名,图片根本就没有上传进去。。。求帮下忙  回复  更多评论
  
# re: 在Struts 2中实现文件上传异常 2010-03-08 16:58 | 苗海峰
2010-3-8 16:48:41 org.apache.struts2.dispatcher.Dispatcher getSaveDir
信息: Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir

希望大家帮忙一下怎么处理  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2010-03-17 16:50 | zm
public void setMyFileContentType(String contentType) {
this .contentType = contentType;
}

public void setMyFileFileName(String fileName) {
this .fileName = fileName;
}
这两个方法名是固定的吗?为什么?  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2010-03-17 16:58 | zm

在此先感谢作者的这篇文章。
上个问题明白了  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2010-07-17 16:27 | LY
你能跳转ACTION吗  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2010-08-22 23:05 | 凤岚舞
@carlos175
这个问题怎么解决啊,  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2010-11-08 15:47 | 广州用户
@carlos175
可以用freemarker,struts2默认有扩展了freemarker,它生成的静态页也容易给百度,google找到。  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2010-11-19 10:15 | yfjtd
try {
in = new BufferedInputStream(new FileInputStream(src),BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
for (int byteRead = 0; (byteRead = in.read(buffer)) > 0; )
{
out.write(buffer, 0, byteRead);
}
} finally
{
if (null != in)
{
in.close();
}
if (null != out)
{
out.close();
}
}
} catch (Exception e)
{
e.printStackTrace();
}

BUFFER_SIZE这个地方报错,不知道咋回事啊?  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2011-01-08 11:58 | yupan6
遇到的问题应该是action中 一些字段里存在的空格引起的问题  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2011-08-17 09:30 | 樱花草
在WebRoot下建立一个UploadImages文件夹试试  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2011-08-17 09:37 | 樱花草
我上传的文件在tomcat中有 但是不显示到我在项目中建立的文件夹中怎么回事  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2011-08-23 14:23 | Blur
@樱花草
这个问题很简单,你的项目是放在myeclipse的工作空间的,图片上传的路径是服务器的路径,所以项目上肯定是没有的,只有tomcat上有图片  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2011-08-31 14:09 | 严学智
java.lang.RuntimeException: Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta) - [unknown location]
at com.opensymphony.xwork2.inject.ContainerBuilder$4.create(ContainerBuilder.java:136)
at com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:476)
at com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:486)
at com.opensymphony.xwork2.inject.ContainerImpl$9.call(ContainerImpl.java:517)
at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:542)
at com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:515)
at org.apache.struts2.dispatcher.Dispatcher.wrapRequest(Dispatcher.java:697)
at org.apache.struts2.dispatcher.FilterDispatcher.prepareDispatcherAndWrapRequest(FilterDispatcher.java:334)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:394)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
at java.lang.Thread.run(Thread.java:595)
Caused by: Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta) - [unknown location]
at org.apache.struts2.config.BeanSelectionProvider$ObjectFactoryDelegateFactory.create(BeanSelectionProvider.java:247)
at com.opensymphony.xwork2.inject.ContainerBuilder$4.create(ContainerBuilder.java:134)
... 20 more
求助。。。。  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2011-10-11 23:54 | spring
缺少common-fileupload.jar@renminyan
  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2011-10-11 23:58 | spring
不要直接把代码复制过去,仔细检查一下,会发现有问题的@li
  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2011-10-25 08:57 | zgb
你好 我是新人 ,希望多多指点,文件上传到什么地方去了,如果我要把文件上传到数据库去 怎么办????  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2011-10-25 09:09 | zgb
@tf
你看下setMyFileFileName有没弄错  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2011-10-25 09:26 | zgb
你好 我是新人 ,希望多多指点,文件上传到什么地方去了,如果我要把文件上传到数据库去 怎么办???? 我的邮箱是zgb6219@163.com 谢谢了。  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2011-10-31 13:55 | zgb
@Allen
你好 说说maximumSize在哪设置30M的大小 啊 谢谢 我一直没弄出来文件大小的设置  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2011-11-23 14:25 | lkang08
博主您好,我照您的方法敲了代码,不过有个问题,就是上传的文件不是存在web根目录下的upload文件夹下的,而是存在
F:\java\2011\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\zxdy\upload
这里。请问一下这是什么原因呢?该怎么解决?
我的FileOutputStream是这样定义的
FileOutputStream fos = new FileOutputStream(ServletActionContext.getServletContext().getRealPath("/upload")+"/"+docFileName);  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2012-04-09 09:04 | Q
@2少
包有冲突  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2012-05-03 16:55 | anshuqing125@sina.com
写的不错。唯一缺点是粘贴后要把所有空格去掉就ok了  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2012-05-21 13:02 | kelvin
写的非常好,正好解决的我的问题,我也是刚开始学struts2,上传一直都报空指针,但是刷新以下后又正常了。后来自己和你的代码核对,发现在setter和getter还有web.xml配置上有出入,然后按照你的修改后,就不会再报空指针错误了,这里特别推荐!!也非常的详细!!  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2012-11-03 15:20 | aaa
不知道为什么,我上传可以成功,但其他文本框的值都取不出来,不知道为什么
  回复  更多评论
  
# re: 在Struts 2中实现文件上传[未登录] 2013-06-07 13:49 | 果果
@referee
手动文件夹建在哪个目录下啊、  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2014-12-31 10:09 | 李雪梅
为什么我能跳到成功页面但是图片不会显示
  回复  更多评论
  
# re: 在Struts 2中实现文件上传 2014-12-31 10:10 | 李雪梅
@果果
在webroot下面一个文件夹  回复  更多评论
  
评论共2页: 1 2 下一页 

只有注册用户登录后才能发表评论。


网站导航: