当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:14:00  来源:本站整理

<b>struts实现多图片上传</b>[Java编程]

赞助商链接



  本文“<b>struts实现多图片上传</b>[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

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;
  }
}


  以上是“<b>struts实现多图片上传</b>[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:

  • <b>hosts是什么 hosts文件在什么位置 若何改正hosts</b>
  • <b>在 Windows 8 中手动安装语言包</b>
  • <b>五个常见 PHP数据库问题</b>
  • Windows中Alt键的12个高效快速的利用本领介绍
  • <b>MySQL ORDER BY 的实现解析</b>
  • <b>详解MySQL存储历程参数有三种范例(in、out、inout)</b>
  • <b>Win8系统恢复出来经典的开始菜单的办法</b>
  • <b>Win8系统花屏怎么办 Win8系统花屏的办理办法</b>
  • <b>Windows 7系统下无线网卡安装</b>
  • <b>为什么 Linux不需求碎片整理</b>
  • <b>Windows 8中删除账户的几种办法(图)</b>
  • <b>教你如安在win7下配置路由器</b>
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .