知识大全 struts实现多图片上传

Posted 限额

篇首语:立志宜思真品格,读书须尽苦功夫。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 struts实现多图片上传相关的知识,希望对你有一定的参考价值。

  package  ninetowns zhangc struts action;

  public class Constant 

  private Constant() 

  

  public static Integer pictureSize =  * * ;   //单位为byte   上传文件最大限额

  public static String  pictureDirectory =  upload\\\\ ;

  

  package  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 HttpServletRequest;

  import javax servlet HttpServletResponse;

  import  apache struts action ActionForm;

  import  apache struts upload FormFile;

  import  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 =  ; 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=pile( \\\\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 = erator();

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

  byte[] buffer = new byte[ ];

  while ((bytesRead = in read(buffer     )) !=  ) 

  out write(buffer    bytesRead);

  

  out close();

  in close();

   catch (Exception e) 

  e printStackTrace();

  

  fileName=fileName+lianjieFileName(rebuildFileName(file getFileName()));

  

  return getFileName(fileName);

  

  

  package  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 HttpServletRequest;

  import javax servlet HttpServletResponse;

  import  apache struts action Action;

  import  apache struts action ActionForm;

  import  apache struts action ActionForward;

  import  apache struts action ActionMapping;

  import  apache struts upload FormFile;

  import  apache struts upload MultipartRequestHandler;

  import  ninetowns zhangc struts form UploadForm;

  /**

  * MyEclipse Struts Creation date: 

  *

  * 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  ninetowns zhangc struts form;

  import javax servlet HttpServletRequest;

  import  apache struts action ActionErrors;

  import  apache struts action ActionForm;

  import  apache struts action ActionMapping;

  /**

  * MyEclipse Struts

  * Creation date: 

  *

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

  

  

  index jsp

  <%@ page language= java  pageEncoding= GBK %>

  <!DOCTYPE HTML PUBLIC  //W C//DTD HTML   Transitional//EN >

  <>

  <head>

  <title>struts upload by zhangc</title>

  </head>

  <body>

  <a >create</a><br>

  </body>

  </>

  new jsp

  <%@ page language= java  pageEncoding= GBK %>

  <%@ taglib uri= /WEB INF/struts bean tld  prefix= bean %>

  <%@ taglib uri= /WEB INF/struts tld  prefix= %>

  <>

  <head>

  <title>struts upload by zhangc</title>

  <script language= javascript >

  var rnum =  ;

  function addRow()

  

  var oT = document getElementById( oTable );

  var newTR = oT insertRow(oT rows length );

  newTR id =  row  + rnum;

  var newTD  = newTR insertCell();

  var newTD  = newTR insertCell();

  newTD setAttribute( colSpan );

  newTD innerText =  file ;

  newTD innerHTML =  <input name=\\ file

  + rnum

  +  \\  type=\\ file\\  > <input name=\\ del\\  type=\\ button\\  value=\\ 删除 \\  onClick=\\ deleteRow(  + rnum + );\\ > ;

  rnum++;

  

  function deleteRow(line)

  

  oTable deleteRow(document getElementById( row  + line) rowIndex);

  

  </script>

  </head>

  <body>

  <form name= updForm  method= post  action= upload do  enctype= multipart/form data >

  <table id= oTable >

  <tr>

  <td>name:</td>

  <td><input name= name  type= text ></td>

  </tr>

  <tr>

  <td>上传图片:</td>

  <td><input type=file name= temp >        <input type= button  name= add  value= 多图片上传   onClick= addRow() ></td>

  </tr>

  <tr>

  <td><input name= sub  type= submit  value=  上传  ></td>

  </tr>

  </table>

  </form>

  </body>

  </>

  next jsp

  <%@ page language= java  import= java util *  pageEncoding= ISO %>

  <%@ taglib prefix= c  uri= java sun /jsp/jstl/core %>

  <%

  String path = request getContextPath() +  /upload ;

  String basePath = request getScheme() +  ://

  + request getServerName() +  :  + request getServerPort()

  + path +  / ;

  %>

  <!DOCTYPE HTML PUBLIC  //W C//DTD HTML   Transitional//EN >

  <>

  <head>

  <base <%=basePath%> >

  <title>My JSP  next jsp  starting page</title>

  <meta  equiv= pragma  content= no cache >

  <meta  equiv= cache control  content= no cache >

  <meta  equiv= expires  content= >

  <meta  equiv= keywords  content= keyword keyword keyword >

  <meta  equiv= description  content= This is my page >

  <!

  <link rel= stylesheet  type= text/css  >

   >

  </head>

  <body>

  <c:forEach var= fileName  items= $fileNames  begin= >

  <img alt=  width=  height=  src= <%=basePath%>$fileName >

  </c:forEach>

  </body>

  </>

  <?xml version=  encoding= UTF ?>

  <!DOCTYPE struts config PUBLIC  //Apache Sofare Foundation//DTD Struts Configuration  //EN   /struts/dtds/struts config_ _ dtd >

  <struts config>

  <data sources />

  <form beans >

  <form bean name= uploadForm  type= ninetowns zhangc struts form UploadForm  />

  </form beans>

  <global exceptions />

  <global forwards />

  <action mappings >

  <action

  input= new jsp

  path= /upload  name= uploadForm

  type= ninetowns zhangc struts action UploadAction

  scope= request >

  <forward

  name= index

  path= /next jsp />

  </action>

  </action mappings>

  <message resources parameter= ninetowns zhangc struts ApplicationResources  />

cha138/Article/program/Java/ky/201311/28037

相关参考

知识大全 struts2文件上传的采用的三种方式解析

   文件上传几乎是每个项目实现的一个必须的模块  上传就是将信息从个人计算机(本地计算机)传递到中央计算机(远程计算机)系统上让网络上的人都能看到将制作好的网页文字图片等发布到互联网上去以

知识大全 基于Struts1.2的动态多文件上传

基于Struts1.2的动态多文件上传  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  首先是we

知识大全 在Struts 2中实现文件上传

在Struts2中实现文件上传  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Struts是通过

知识大全 Struts上传多个文件的例子

J2EE综合:Struts上传多个文件的例子  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!&nbs

知识大全 演示Struts2实现简单上传代码

演示Struts2实现简单上传代码  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  webxml&

知识大全 分享:用Struts上传多个文件的方法

分享:用Struts上传多个文件的方法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  最近在做S

知识大全 在百度点图片搜寻出来的图片是怎么上传上去的

在百度点图片搜寻出来的图片是怎么上传上去的?我也想上传?请高手指点谢谢!这没法上传他搜的图片都是别的网站上的根据图片的名字如果你想被搜到,可以上传到百度空间,注意图片描述要清楚,不过即使这样也不能保证

知识大全 使用Struts+Hibernate上传大对象(BLOB)

使用Struts+Hibernate上传大对象(BLOB)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一

知识大全 为什么我的百度图片里面不能上传图片呢

为什么我的百度图片里面不能上传图片呢?别人的都有一个上传图片的按钮,怎么我的就没有呢?希望大神解答请问是电脑上的吗?百度图片可以上传图片寻找相似图片,步骤:进入百度图片,然后在搜寻栏右侧有个相机,就是

知识大全 我要上传图片到百度

我要上传图片到百度点击图片就可以上传了,只能传本地图片你好!要想让自己的照片在百度被搜到,具体方法如下首先你可以把照片上传到百度空间的公开相册里,就能在百度里通过搜索找到你的照片。进入百度图片:ima