posts - 310, comments - 6939, trackbacks - 0, articles - 3
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Struts2上传文件示例

Posted on 2007-10-23 12:22 诗特林 阅读(25066) 评论(20)  编辑  收藏 所属分类: Struts
                                             Struts2上传文件示例

源代码:Struts2Upload.rar

1.包如下:请自行下载


2.Action类

package com.sterning;

import java.io.File;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class StrutsFileUpload extends ActionSupport implements
        ServletContextAware 
{

    
private File upload;// 实际上传文件

    
private String uploadContentType; // 文件的内容类型

    
private String uploadFileName; // 上传文件名

    
private String fileCaption;// 上传文件时的备注

    
private ServletContext context;

    
public String execute() throws Exception {

        
try {
            
            String targetDirectory 
= context.getRealPath("/upload");
            String targetFileName 
= uploadFileName;
            File target 
= new File(targetDirectory, targetFileName);
            FileUtils.copyFile(upload, target);            
            
            setUploadFileName(target.getPath());
//保存文件的存放路径
        }
 catch (Exception e) {

            addActionError(e.getMessage());

            
return INPUT;
        }


        
return SUCCESS;

    }


    
public String getFileCaption() {
        
return fileCaption;
    }


    
public void setFileCaption(String fileCaption) {
        
this.fileCaption = fileCaption;
    }


    
public File getUpload() {
        
return upload;
    }


    
public void setUpload(File upload) {
        
this.upload = upload;
    }


    
public String getUploadContentType() {
        
return uploadContentType;
    }


    
public void setUploadContentType(String uploadContentType) {
        
this.uploadContentType = uploadContentType;
    }


    
public String getUploadFileName() {
        
return uploadFileName;
    }


    
public void setUploadFileName(String uploadFileName) {
        
this.uploadFileName = uploadFileName;
    }


    
public void setServletContext(ServletContext context) {
        
this.context = context;
    }


}


3.页面

上传页面:upload.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"%>   
<%@ taglib prefix="s" uri="/struts-tags" %>   
<html>
    
<head>
        
<title>文件上传示例</title>
        
<link href="<s:url value="/css/main.css"/>" rel="stylesheet"
            type="text/css" />

    
</head>

    
<body>

        
<s:actionerror />
        
<s:fielderror />
        
<s:form action="doUpload" method="POST" enctype="multipart/form-data">
            
<tr>
                
<td colspan="2">
                    
<h1>
                        文件上传示例
                    
</h1>
                
</td>
            
</tr>

            
<s:file name="upload" label="上传的文件" />
            
<s:textfield name="fileCaption" label="备注" />
            
<s:submit value="上   传"/>
        
</s:form>
    
</body>
</html>

上传成功页面:upload_success.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"%>  
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
    
<head>
        
<title>上传成功</title>
        
<link href="<s:url value="/css/main.css"/>" rel="stylesheet"
            type="text/css" />
    
</head>

    
<body>
        
<table class="wwFormTable">
            
<tr>

                
<td colspan="2">
                    
<h1>
                        上传成功
                    
</h1>
                
</td>
            
</tr>

            
<tr>
                
<td class="tdLabel">
                    
<label for="doUpload_upload" class="label">
                        内容类型:
                    
</label>
                
</td>
                
<td>
                    
<s:property value="uploadContentType" />
                
</td>
            
</tr>

            
<tr>
                
<td class="tdLabel">
                    
<label for="doUpload_upload" class="label">
                        文件路径:
                    
</label>
                
</td>
                
<td>
                    
<s:property value="uploadFileName" />
                
</td>
            
</tr>


            
<tr>
                
<td class="tdLabel">
                    
<label for="doUpload_upload" class="label">
                        临时文件:
                    
</label>
                
</td>
                
<td>
                    
<s:property value="upload" />
                
</td>
            
</tr>

            
<tr>
                
<td class="tdLabel">
                    
<label for="doUpload_upload" class="label">
                        备注:
                    
</label>
                
</td>
                
<td>
                    
<s:property value="fileCaption" />
                
</td>
            
</tr>


        
</table>

    
</body>
</html>

4.struts.xml
<?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>
    
<constant name="struts.devMode" value="true" />
    
<constant name="struts.i18n.encoding" value="GB2312" />
 
    
<package name="NG" namespace="/" extends="struts-default">
        
<action name="showUpload">
            
<result>/upload.jsp</result>
        
</action>
        
        
<action name="doUpload" class="com.sterning.StrutsFileUpload">
            
<result name="input">/upload.jsp</result>
            
<result>/upload_success.jsp</result>
        
</action>
    
</package>

</struts>


5.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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>customization</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>

</web-app>




评论

# re: Struts2上传文件示例  回复  更多评论   

2007-10-24 12:03 by 快译站
总结的不错
java集中营http://java.co.cc诚意邀请您做开源技术struts2.0

同时欢迎您加入java技术QQ群23133419

# re: Struts2上传文件示例  回复  更多评论   

2007-10-24 12:05 by 快译站
java集中营地址 http://java8.co.cc

# re: Struts2上传文件示例[未登录]  回复  更多评论   

2007-10-26 09:59 by apple0668
下载源码加进相关的包,一提交就发射404错误!不知道是什么原因!连包跟源码发我一份,谢谢!email:chensp1230@163.com

# re: Struts2上传文件示例  回复  更多评论   

2007-11-14 11:19 by study
下载源码加进相关的包(除commons-fileupload-1.1.1 ,commons-io-1.1的版本有点区别之外,其它完全相同)一提交就产生404错误!不知什么原因!能否连包跟源码发我一份,谢谢! email:zggzspring@163.com

# re: Struts2上传文件示例  回复  更多评论   

2007-11-25 16:56 by shaomin
不错
谢谢分享
下来试试

# re: Struts2上传文件示例  回复  更多评论   

2008-01-04 21:43 by 凌晨风
楼主写的不错,这个例子好像是Struts包里的源码,但是我不知道上传成功后怎样看这个文件,比如我传了一张图片,我怎样才能看到我传的图片呢,上传的是一个tmp的虚拟文件,我也弄不清楚,帮忙啊!

# re: Struts2上传文件示例[未登录]  回复  更多评论   

2008-01-17 09:17 by yang
请问如何控制上传文件的类型和大小! 谢谢!!!

# re: Struts2上传文件示例[未登录]  回复  更多评论   

2008-01-17 17:04 by yang
请问如何控制上传文件的类型和大小! 谢谢!!!

# re: Struts2上传文件示例  回复  更多评论   

2008-03-21 10:08 by 花花
请问要上传视频文件用这个能行吗
视频至少都有5、6M的 这个好像只能传2M以内的
怎么能把限制设大点儿 或者去掉限制?

# re:   回复  更多评论   

2009-03-18 11:35 by fjj
楼主,能把Struts2上传文件示例的所需jar发给我不?谢谢!
fanjj_023@126.com

# re: Struts2上传文件示例  回复  更多评论   

2009-03-19 16:41 by fanjj_023
楼主,为什么我测试时tomcat会报/WEB-INF/web.xml not found,还有jsp也报异常,提示下面这句
<link href=" <s:url value="/css/examplecss"/>"rel="stylesheet" type="text/css"/> 为什么呀?

# re: Struts2上传文件示例  回复  更多评论   

2009-05-07 10:50 by 打哈十分客户
一个个只会叫,都不会自己想办法啊!!!lz讲的很透彻!!!

# re: Struts2上传文件示例[未登录]  回复  更多评论   

2009-05-19 11:19 by aaa
看到 上面那些 有错误的.感觉好笑,

这么差的自学能力,还学什么 Java ?

LZ 的这个例子,应用太简单了,文件类型,大小,多文件的上传

以及 Action 类里面引用了 Servlet ,这些都还没处理呀...

# re: Struts2上传文件示例  回复  更多评论   

2009-05-27 12:05 by ss
水平太低不懂就问,有错吗?难道你们一生下来就会..从没问过一些简单的问题?

# re: Struts2上传文件示例  回复  更多评论   

2009-06-10 22:02 by kinble
不知道为什么总是报错:
java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
通过网上查找,都说是缺少了commons-io包,但我很肯定我是已经导进去的了!

# re: Struts2上传文件示例  回复  更多评论   

2010-02-24 11:20 by 一根烟
借鉴了你的代码 谢谢

# re: Struts2上传文件示例  回复  更多评论   

2010-05-31 15:31 by 张三
可以啊

# re: Struts2上传文件示例  回复  更多评论   

2011-05-19 22:54 by ....
不就是把struts2教程里的东西拷进来了嘛,又不是你自己写的

# re: Struts2上传文件示例[未登录]  回复  更多评论   

2012-02-14 15:39 by kevin
测试好用 攒人品感谢楼主~~

# re: Struts2上传文件示例  回复  更多评论   

2012-03-16 08:15 by zhen
Java程序员之家 QQ:群号2218986 欢迎大家加入

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


网站导航: