WEB开发网
开发学院软件开发Java 多图片上传(struts实现) 阅读

多图片上传(struts实现)

 2009-09-22 00:00:00 来源:WEB开发网   
核心提示:packagecom.ninetowns.zhangc.struts.action;publicclassConstant{privateConstant(){}publicstaticIntegerpictureSize=10*1024*1024;//单位为byte上传文件最大限额;publicstaticStrin

package com.ninetowns.zhangc.struts.action;
public class Constant {
 
 private Constant() {
  
 }
 public static Integer pictureSize = 10*1024*1024;   //单位为byte   上传文件最大限额;
 public static String  pictureDirectory = "upload\\";
}

package com.ninetowns.zhangc.struts.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;
public class PictureUpLoad {
 private PictureUpLoad() {
 }
 private static boolean isValidFile(String fileName) {
  String[] validFiles = { "gif", "jpg", "jpeg", "jpe", "bmp", "png","txt/plain","text","doc","rar" };
  boolean ret = false;
  for (int i = 0; i < validFiles.length; i++) {
   if (fileName.toLowerCase().endsWith(validFiles[i])) {
    ret = true;
    break;
   }
  }
  return ret;
 }
 private static String rebuildFileName(String fileName) {
  java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
    "yyyyMMddHHmmss");
  java.util.Date date = new java.util.Date();
  return sdf.format(date) + "_" + fileName;
 }
 
 private static String lianjieFileName(String fileName){
  return " "+fileName;
 }
 private static boolean isDirectoryExists(String path){
  java.io.File file = new File(path);
  if(!file.exists()){
     return file.mkdir();
  }else
   return true;
 }
 private static String[] getFileName(String filesName){
   Pattern x=Pattern.compile("\\w*\\s+\\w*");  
    String[] fileName = filesName.split("\\s+");
    Matcher t = x.matcher(filesName);
    System.out.println(  t.matches());
  return fileName;
 }
 
 public static String[] fileUpLoad(ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  String path = "";
  String fileName = "";
  MultipartRequestHandler handle = form.getMultipartRequestHandler();
  Hashtable hashTable = handle.getFileElements();
  Collection cln = hashTable.values();
  Iterator iterator = cln.iterator();
  while (iterator.hasNext()) {
   FormFile file = (FormFile) iterator.next();
   path = request.getRealPath("/") + Constant.pictureDirectory;
   if(isDirectoryExists(path)==false) return null;
   if (file.getFileName() == null)
    return null;
   if (file.getFileSize() > Constant.pictureSize)
    return null;
   if (!isValidFile(file.getContentType()))
    return null;
   try {
    InputStream in = file.getInputStream();
    OutputStream out = new FileOutputStream(path
      + rebuildFileName(file.getFileName()));// "/"表示保存的路径可以修改为"load/"
    int bytesRead = 0;
    byte[] buffer = new byte[8192];
    while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
     out.write(buffer, 0, bytesRead);
    }
    out.close();
    in.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
   fileName=fileName+lianjieFileName(rebuildFileName(file.getFileName()));
  }
  return getFileName(fileName);
 }
}

package com.ninetowns.zhangc.struts.action;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;
import com.ninetowns.zhangc.struts.form.UploadForm;
/**
 * MyEclipse Struts Creation date: 10-18-2006
 *
 * XDoclet definition:
 *
 * @struts.action input="new.jsp" validate="true"
 * @struts.action-forward name="list" path="list.jsp" redirect="true"
 */
public class UploadAction extends Action {
  /*
   * Generated Methods
   */
  /**
   * Method execute
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return ActionForward
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
   // TODO Auto-generated method stub
   UploadForm updForm = (UploadForm)form;
   try {
  String[] fileNames = PictureUpLoad.fileUpLoad(updForm, request, response);
  request.setAttribute("fileNames", fileNames);
  System.out.println("========="+fileNames.length);
  for(String file:fileNames){
   System.out.println("filename   "+file);
  }
 } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
   return mapping.findForward("index");
  }
 }
 
package com.ninetowns.zhangc.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
 * MyEclipse Struts
 * Creation date: 10-18-2006
 *
 * XDoclet definition:
 * @struts.form name="uploadForm"
 */
public class UploadForm extends ActionForm {
 /*
  * Generated fields
  */
 /** name property */
 private String name;
 /*
  * Generated Methods
  */
 /**
  * Method validate
  * @param mapping
  * @param request
  * @return ActionErrors
  */
 public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {
  // TODO Auto-generated method stub
  return null;
 }
 /**
  * Method reset
  * @param mapping
  * @param request
  */
 public void reset(ActionMapping mapping, HttpServletRequest request) {
  // TODO Auto-generated method stub
 }
 /**
  * Returns the name.
  * @return String
  */
 public String getName() {
  return name;
 }
 /**
  * Set the name.
  * @param name The name to set
  */
 public void setName(String name) {
  this.name = name;
 }
}

1 2 3  下一页

Tags:图片 上传 struts

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接