struts2的文件上传

一直以来自己都是看书的时候多,而实际做却很少,
最近慢慢开始做东西了,发现,看与做是2个完全不同的事情。
记录下面的东西是为了以后使用方便。
利用struts2上传文件。
上传页面
       <form action="upload" method="post" enctype="multipart/form-data">
           文件标题:
<input type="text" name="title" /><br>
           选择文件:
<input type="file" name="upload" /><br>
           
<input value="上传" type="submit" />
       
</form>
然后就是struts.xml文件,在  <package>标签中
        <action name="upload" class="com.duduli.li.Upload">
            
<param name="savePath">/file</param>
            
<result name="showUpload">showUpload.jsp</result>
        
</action>
然后就是处理上传的文件。
package com.duduli.li;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings(
"serial")
public class Upload extends ActionSupport {
    
private String title;
    
private File upload;
    
private String uploadContextType;
    
private String uploadFileName;
    
private String savePath;

    
public String getTitle() {
        
return title;
    }

    
public void setTitle(String title) {
        
this.title = title;
    }

    
public File getUpload() {
        
return upload;
    }

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

    
public String getUploadContextType() {
        
return uploadContextType;
    }

    
public void setUploadContextType(String uploadContextType) {
        
this.uploadContextType = uploadContextType;
    }

    
public String getUploadFileName() {
        
return uploadFileName;
    }

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

//通过struts2的配置文件得到上传目录,这个是很重要的
    @SuppressWarnings("deprecation")
    
public String getSavePath() {
        
return ServletActionContext.getRequest().getRealPath(savePath);
    }

    
public void setSavePath(String value) {
        
this.savePath = value;
    }

    @Override
    
public String execute() throws Exception {
        FileOutputStream fos 
= new FileOutputStream(getSavePath() + "\\"
                
+ getUploadFileName());
        FileInputStream fis 
= new FileInputStream(getUpload());
        
byte[] buffer = new byte[1024];
        
int len = 0;
        
while ((len = fis.read(buffer)) > 0) {
            fos.write(buffer, 
0, len);
        }
        
return "showUpload";
    }
}

如果你认为配置这些就可以了,那你就错了,还需要配置一个struts.properties文件。定义一个临时的上传文件夹。
#配置临时存放目录
struts.multipart.saveDir = /tmp
如果tmp文件夹不存在的话,会自动建立,一般在磁盘根目录
还有一个就是简单的显示上传成功的页面。
<%@ page language="java"  pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>  
    
<title>上传成功</title>
  
</head>
  
  
<body>
      上传成功!
      
<s:property value="title"/>
  
</body>
</html>

posted on 2009-05-06 22:19 duduli 阅读(2063) 评论(1)  编辑  收藏 所属分类: SSH/SSH2

评论

# re: struts2的文件上传 2012-01-10 23:34 11

111  回复  更多评论   


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


网站导航:
 
<2009年5月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

公告

welcome to my place.

常用链接

留言簿(5)

我参与的团队

随笔分类

随笔档案

新闻分类

石头JAVA摆地摊儿

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜

@duduli