Struts2 文件上传

jsp页面如下:

<%@ page language="java" import="java.util.*,java.io.*" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</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>

action处理页面如下:

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 File myFile;             //上传的文件
 public void setMyFile(File myFile)  {
        this.myFile = myFile;
   }

 private String   myFileContentType;          // 上传文件类型
    private String myFileFileName;          // 上传文件名
    private String imageFileName;    //重命名文件
    private String caption;
  
    public String getCaption() {
  return caption;
 }

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

 public void setImageFileName(String imageFileName) {
  this.imageFileName = imageFileName;
 }

 public void setMyFileContentType(String contentType)  {
        this.myFileContentType = contentType;
   }
  
    public void setMyFileFileName(String fileName)  {
        this.myFileFileName = fileName;
   }
      
    
    public File getMyFile() {
  return myFile;
 }
    public String getImageFileName()  {
        return imageFileName;
   }
    

    private  void copy(File src, File dst)  {   //源文件拷貝到目標文件
        try  {
           InputStream in = null ;
           OutputStream out = null ;
            try  {               
               in = new BufferedInputStream( new FileInputStream(src), 1024);
               out = new BufferedOutputStream( new FileOutputStream(dst), 1024);
                byte [] buffer = new byte [1024];
                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  String getExtention(String fileName)  {     //取得文件的擴展名
        int pos = fileName.lastIndexOf(".");
        return fileName.substring(pos);
   } */

   @Override
    public String execute()      {       
//       imageFileName = new Date().getTime() + getExtention(myFileFileName);   //對文件重命名。
       File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/upload") + "/" + myFileFileName);//源文件同名文件保存到context下upload文件夾中。
       copy(myFile, imageFile);  
        return SUCCESS;
   }
}

posted on 2011-04-29 13:10  ranran2010  阅读(351)  评论(0编辑  收藏  举报