sturts2实现上传

package or

导入架包:

commons-fileupload-1.2.1.jar

commons -io.jar

g.fzrb.newsmanager.action.struts;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.hi.SpringContextHolder;
import org.hi.common.attachment.action.cust.FtpUtil;
import org.hi.common.attachment.model.Attachment;
import org.hi.common.attachment.service.AttachmentManager;
import org.hi.framework.HiConfigHolder;
import org.hi.framework.web.BusinessException;
import org.hi.i18n.util.I18NUtil;

import com.opensymphony.xwork2.ActionSupport;

public class UpLoadAction extends ActionSupport {
 private File[] image;
 private String[] imageContentType; // 文件的内容类型
 private String[] imageFileName; // 上传文件名
 private List<Attachment> imageFiles = new ArrayList<Attachment>();

 /**
  * @return the image
  */
 public File[] getImage() {
  return image;
 }

 /**
  * @param image
  *            the image to set
  */
 public void setImage(File[] image) {
  this.image = image;
 }

 /**
  * @return the imageContentType
  */
 public String[] getImageContentType() {
  return imageContentType;
 }

 /**
  * @param imageContentType
  *            the imageContentType to set
  */
 public void setImageContentType(String[] imageContentType) {
  this.imageContentType = imageContentType;
 }

 /**
  * @return the imageFileName
  */
 public String[] getImageFileName() {
  return imageFileName;
 }

 /**
  * @param imageFileName
  *            the imageFileName to set
  */
 public void setImageFileName(String[] imageFileName) {
  this.imageFileName = imageFileName;
 }

 /**
  * @return the imageFiles
  */
 public List<Attachment> getImageFiles() {
  return imageFiles;
 }

 /**
  * @param imageFiles
  *            the imageFiles to set
  */
 public void setImageFiles(List<Attachment> imageFiles) {
  this.imageFiles = imageFiles;
 }

 /**
  * @return the maxSize
  */
 public String getMaxSize() {
  return maxSize;
 }

 /**
  * @param maxSize
  *            the maxSize to set
  */
 public void setMaxSize(String maxSize) {
  this.maxSize = maxSize;
 }

 public static String getExt(String fileName)

 {

  return fileName.substring(fileName.lastIndexOf("."));

 }

 private String maxSize = HiConfigHolder
   .getProperty("hi.upload.ftp.maxSize") == null ? "100"
   : HiConfigHolder.getProperty("hi.upload.ftp.maxSize");
 private static final int BUFFER_SIZE = 16 * 1024;
 AttachmentManager attachmentMgr = (AttachmentManager) SpringContextHolder
   .getBean(Attachment.class);

 public String execute() throws Exception {
  // 原文件
  File[] src = this.getImage();
  System.out.println(src.length);
  Attachment atta = null;
  // 目标地址
  String dstPath = ServletActionContext.getServletContext().getRealPath(
    "/")
    + "upload";
  String disp = "/upload/attachment/";
  String imageFileName = "";
  String type = "";
  // 循环存放原文件
  for (int i = 0; i < src.length; i++) {
   atta = new Attachment();
   // 给源文件重新名称
   type = this.getImageContentType()[i] + type;
   type += ",";
   String na = getImageFileName()[i];
   imageFileName = imageFileName + na;
   imageFileName += ",";
   ServletActionContext.getRequest().setAttribute(
     "imageFileName" + (i + 1), na);
   // 声明目标文件
   File dst = new File( dstPath+ "/" + imageFileName);
   // 将源文件拷贝到目标地
   copy(src[i], dst);
  }
  System.out.println(imageFileName);
  atta.setFileType(type);
  atta.setFileName(imageFileName);
  atta.setAttachmentPath(disp + imageFileName);
  attachmentMgr.saveAttachment(atta);
  return SUCCESS;
 }
 // 将指定的文件拷贝到指定的目录
 public static void copy(File src, File dst) {
  InputStream in = null;
  OutputStream out = null;
  try {
   in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
   System.out.println(src);
   out = new BufferedOutputStream(new FileOutputStream(dst),
     BUFFER_SIZE);
   byte[] b = new byte[1024];
   int len = 0;
   while ((len = in.read(b)) != -1) {
    out.write(b, 0, len);
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    if (null != in) {
     in.close();
    }
    if (null != out) {
     out.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

 // 根据当前时间给文件取指定的名字
 public static String getImageFileName(String fileName) {
  Random rnd = new Random();
  int rn = rnd.nextInt(1000);
  return new Date().getTime() + rn + getExtention(fileName);
 }

 // 获取文件的后缀
 public static String getExtention(String fileName) {
  int pos = fileName.lastIndexOf(".");
  return fileName.substring(pos);
 }

}

 

 

 


 public InputStream getDownloadFile(){
        return ServletActionContext.getServletContext().getResourceAsStream("/upload/Struts2.ppt");
       
    }

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
//下载的配置
 <action name="download" class="com.demo.action.DownloadAction">
            <result name="success" type="stream">
               <param name="contentType">application/vnd.ms-powerpoint</param>
               <param name="contentDisposition">filename="Struts2.ppt"</param>
               <param name="inputName">downloadFile</param>
            </result>
        </action>
//上传的配置
<action name="upload" class="com.demo.action.UploadAction">
            <result name="success">/uploadsuccess.jsp</result>
            <result name="input">/upload.jsp</result>
            <interceptor-ref name="fileUpload">
                <param name="maximumSize">4096000</param>
                <param name="allowedTypes">application/vnd.ms-powerpoint</param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
        </action>

 

你可能感兴趣的:(exception,image,String,File,buffer,DST)