您现在的位置是:首页 >

如何把图片上传到文件里 struts实现多图片上传

火烧 2022-12-10 17:06:54 1053
trut 实现多图片上传    ackage  i etow zha gc trut actio    u lic cla  Co ta t {   rivate Co ta t  {  }   u

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>

如何把图片上传到文件里 struts实现多图片上传

  <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  />

lishixinzhi/Article/program/Java/ky/201311/28037  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码