熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

J2EE綜合:Struts上傳多個文件的例子

2022-06-13   來源: Java開源技術 
    最近一個基於Struts的項目中要用到上傳多張圖片到系統中我在網上找了一下發現大多數都是轉載的例子對項目的作用不大故寫下了這個例子希望對有用到的朋友有所幫助要求JDK以上lib庫要包含如下jar文件antlrjarcommonsbeanutilsjarcommonscollectionsjarcommonsdigesterjarcommonsfileuploadjarcommonsloggingjarcommonsvalidatorjarjakartaorojarstrutsjar這些都是Struts中的jar文件以及Struts中的標簽庫文件

  一webxml文件

  < ?xml version= encoding=UTF?>

  < !DOCTYPE webapp

  PUBLIC //Sun Microsystems Inc//DTD Web Application //EN

  app__dtd>

  < webapp>

  < servlet>

  < servletname>actionServlet< /servletname>

  < servletclass>orgapachestrutsactionActionServlet< /servletclass>

  < initparam>

  < paramname>config< /paramname>

  < paramvalue>/WEBINF/strutsconfigxml< /paramvalue>

  < /initparam>

  < initparam>

  < paramname>debug< /paramname>

  < paramvalue>< /paramvalue>

  < /initparam>

  < loadonstartup>< /loadonstartup>

  < /servlet>

  < ! struts actionServlet mapping>

  < servletmapping>

  < servletname>actionServlet< /servletname>

  < urlpattern>*do< /urlpattern>

  < /servletmapping>

  < ! welcome file list >

  < welcomefilelist>

  < welcomefile>indexjsp< /welcomefile>

  < /welcomefilelist>

  < ! error handle >

  < errorpage>

  < errorcode>< /errorcode>

  < location>/errorjsp< /location>

  < /errorpage>

  < /webapp>

  二strutsconfigxml文件

  < ?xml version= encoding=ISO ?>

  < !DOCTYPE strutsconfig PUBLIC

  //Apache Software Foundation//DTD Struts Configuration //EN

  config__dtd>

  < strutsconfig>

  < ! form beans >

  < formbeans>

  < formbean name=uploadForm type=orgapachestrutsactionDynaActionForm>

  < formproperty name=type type =javalangString[]/>

  < formproperty name=name type =javalangString[]/>

  < formproperty name=file type =orgapachestrutsuploadFormFile/>

  < formproperty name=file type =orgapachestrutsuploadFormFile/>

  < formproperty name=file type =orgapachestrutsuploadFormFile/>

  < formproperty name=file type =orgapachestrutsuploadFormFile/>

  < formproperty name=file type =orgapachestrutsuploadFormFile/>

  < formproperty name=file type =orgapachestrutsuploadFormFile/>

  < formproperty name=file type =orgapachestrutsuploadFormFile/>

  < formproperty name=file type =orgapachestrutsuploadFormFile/>

  < formproperty name=file type =orgapachestrutsuploadFormFile/>

  < formproperty name=file type =orgapachestrutsuploadFormFile/>

  < /formbean>

  < /formbeans>

  < ! forward >

  < globalforwards>

  < forward name= successed path=/indexjsp>< /forward>

  < forward name= failed path=/errorjsp>< /forward>

  < /globalforwards>

  < ! action handle>

  < actionmappings>

  < action path=/uploaded type=comfangchuangactionUploadAction

  name=uploadForm scope=request input=/uploadjsp>

  < forward name=uploaded path=/uploadjsp>< /forward>

  < /action>

  < /actionmappings>

  < /strutsconfig>

  三uploadjsp文件

  < %@ page language=java import=javautil* pageEncoding=gb errorPage=errorjsp%>

  < %@ taglib uri=/WEBINF/strutshtmltld prefix=html%>

  < %@ taglib uri=/WEBINF/strutsbeantld prefix=bean%>

  < %@ taglib uri=/WEBINF/strutslogictld prefix=logic%>

  < %@ taglib uri=/WEBINF/strutstilestld prefix=title%>

  >

  < html>

  < head>

  < title>上傳多個文件的jsp文件< /title>

  < meta httpequiv=pragma content=nocache>

  < meta httpequiv=cachecontrol content=nocache>

  < meta httpequiv=expires content=>

  < meta httpequiv=keywords content=struts多個文件上傳>

  < meta httpequiv=description content=This is a test>

  < /head>

  < body>

  < html:form action=uploadeddo enctype=multipart/formdata method=post>

  < table border= width=% align=center>

  < tr bgColor=#bcff>

  < td align=left>文件類型< /td>

  < td align=left>文件描述< /td>

  < td align=left>文件名< /td>

  < /tr>

  < tr>

  < td align=left>< select name=type>

  < option value= selected>湖南省< /option>

  < option value=>湖北省< /option>

  < option value=>廣東省< /option>

  < option value=>北京市< /option>

  < option value=>上海市< /option>

  < /select>< /td>

  < td align=left>< input type=text name=name value=>< /td>

  < td align=left>< input type=file name=file size= value=>

  < /td>

  < /tr>

  < tr>

  < td align=left>< select name=type>

  < option value=selected>湖南省< /option>

  < option value=>湖北省< /option>

  < option value=>廣東省< /option>

  < option value=>北京市< /option>

  < option value=>上海市< /option>

  < /select>< /td>

  < td align=left>< input type=text name=name value=>< /td>

  < td align=left>< input type=file name=file size= value=>

  < /td>

  < /tr>

  < tr>

  < td align=left>< select name=type>

  < option value=>湖南省< /option>

  < option value=selected>湖北省< /option>

  < option value=>廣東省< /option>

  < option value=>北京市< /option>

  < option value=>上海市< /option>

  < /select>< /td>

  < td align=left>< input type=text name=name value=>< /td>

  < td align=left>< input type=file name=file size= value=>

  < /td>

  < /tr>

  < tr>

  < td align=left>< select name=type>

  < option value=>湖南省< /option>

  < option value=selected>湖北省< /option>

  < option value=>廣東省< /option>

  < option value=>北京市< /option>

  < option value=>上海市< /option>

  < /select>< /td>

  < td align=left>< input type=text name=name value=>< /td>

  < td align=left>< input type=file name=file size= value=>

  < /td>

  < /tr>

  < tr>

  < td align=left>< select name=type>

  < option value=>湖南省< /option>

  < option value=>湖北省< /option>

  < option value=selected>廣東省< /option>

  < option value=>北京市< /option>

  < option value=>上海市< /option>

  < /select>< /td>

  < td align=left>< input type=text name=name value=>< /td>

  < td align=left>< input type=file name=file size= value=>

  < /td>

  < /tr>

  < tr>

  < td align=left>< select name=type>

  < option value=>湖南省< /option>

  < option value=>湖北省< /option>

  < option value=selected>廣東省< /option>

  < option value=>北京市< /option>

  < option value=>上海市< /option>

  < /select>< /td>

  < td align=left>< input type=text name=name value=>< /td>

  < td align=left>< input type=file name=file size= value=>

  < /td>

  < /tr>

  < tr>

  < td align=left>< select name=type>

  < option value=>湖南省< /option>

  < option value=>湖北省< /option>

  < option value=>廣東省< /option>

  < option value=selected>北京市< /option>

  < option value=>上海市< /option>

  < /select>< /td>

  < td align=left>< input type=text name=name value=>< /td>

  < td align=left>< input type=file name=file size= value=>

  < /td>

  < /tr>

  < tr>

  < td align=left>< select name=type>

  < option value=>湖南省< /option>

  < option value=>湖北省< /option>

  < option value=>廣東省< /option>

  < option value=selected>北京市< /option>

  < option value=>上海市< /option>

  < /select>< /td>

  < td align=left>< input type=text name=name value=>< /td>

  < td align=left>< input type=file name=file size= value=>

  < /td>

  < /tr>

  < tr>

  < td align=left>< select name=type>

  < option value=>湖南省< /option>

  < option value=>湖北省< /option>

  < option value=>廣東省< /option>

  < option value=>北京市< /option>

  < option value=selected>上海市< /option>

  < /select>< /td>

  < td align=left>< input type=text name=name value=>< /td>

  < td align=left>< input type=file name=file size= value=>

  < /td>

  < /tr>

  < tr>

  < td align=left>< select name=type>

  < option value=>湖南省< /option>

  < option value=>湖北省< /option>

  < option value=>廣東省< /option>

  < option value=>北京市< /option>

  < option value=selected>上海市< /option>

  < /select>< /td>

  < td align=left>< input type=text name=name value=>< /td>

  < td align=left>< input type=file name=file size= value=>

  < /td>

  < /tr>

  < tr>

  < td align=left> < /td>

  < td align=left> < /td>

  < td align=left>< input type=submit value=上傳文件 value=submit

  onclick=return(confirm(你確認要上傳文件嗎?))>< /td>

  < /tr>

  < /table>

  < /html:form>

  < /body>

  < /html>

  四還有兩個簡單jsp文件indexjsperrorjsp可以自己寫

  五action文件

  package comfangchuangaction;

  /**

  * @author anEngineer 處理上傳文件action

  */

  public class UploadAction extends Action {

  public ActionForward execute(

  ActionMapping mapping

  ActionForm form

  HttpServletRequest request

  DynaActionForm fileForm = (DynaActionForm) form;

  String[] types = (String[]) fileFormget(type);

  String[] names = (String[]) fileFormget(name);

  FormFile file = (FormFile) fileFormget(file);

  FormFile file = (FormFile) fileFormget(file);

  FormFile file = (FormFile) fileFormget(file);

  FormFile file = (FormFile) fileFormget(file);

  FormFile file = (FormFile) fileFormget(file);

  FormFile file = (FormFile) fileFormget(file);

  FormFile file = (FormFile) fileFormget(file);

  FormFile file = (FormFile) fileFormget(file);

  FormFile file = (FormFile) fileFormget(file);

  FormFile file = (FormFile) fileFormget(file);

  Map< String FormFile> fileMap = new HashMap< String FormFile>();

  fileMapput(file + * + types[] + * + names[] file);

  fileMapput(file + * + types[] + * + names[] file);

  fileMapput(file + * + types[] + * + names[] file);

  fileMapput(file + * + types[] + * + names[] file);

  fileMapput(file + * + types[] + * + names[] file);

  fileMapput(file + * + types[] + * + names[] file);

  fileMapput(file + * + types[] + * + names[] file);

  fileMapput(file + * + types[] + * + names[] file);

  fileMapput(file + * + types[] + * + names[] file);

  fileMapput(file + * + types[] + * + names[] file);

  Set fileSet = fileMapentrySet();

  Iterator iter = erator();

  // 取當前系統路徑E:\Tomcat\webapps\strutsUpload\ 其中strutsUpload為當前context

  String filePath = thisgetServlet()getServletContext()getRealPath(/);

  // 保存文件的文件夾

  File savePath = new File(filePath + UploadFiles\\);

  filePath = filePath+ UploadFiles\\;

  if (!savePathexists()) {

  savePathmkdir();

  }

  while (iterhasNext()) {

  MapEntry unit = (MapEntry) iternext();

  String key = (String) unitgetKey();

  FormFile file = (FormFile) unitgetValue();

  //文件大小符合要求且是圖片文件

  if ((filegetFileSize() >= )&& DealPhotoisPhoto(file)) {

  //圖片類別

  String photoType = keysubstring(keyindexOf(*) + key

  lastIndexOf(*));

  //圖片描述

  String photoName = keysubstring(keylastIndexOf(*) + key

  length());

  //存數據庫操作在數據庫中保存文件的名稱類型及在服務器上的相對路徑

  //

  //判斷是否重名

  if(DealPhotoisFileExist(filegetFileName()filePath))

  DealPhotorename(filegetFileName()filePath);

  try {

  InputStream stream = filegetInputStream();// 把文件讀入

  // 建立一個上傳文件的輸出流

  OutputStream bos = new FileOutputStream(filePath+filegetFileName());

  int bytesRead = ;

  byte[] buffer = new byte[];

  while ((bytesRead = streamread(buffer )) != ) {

  boswrite(buffer bytesRead);// 將文件寫入服務器

  }

  bosclose();

  streamclose();

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  }

  return mappingfindForward(uploaded);

  //還有其他可以改正的地方如錯誤信息提示把照片處理函數放到一個公用類寫文件操作等

  }

  }

  六照片處理類文件

  package comfangchuangrun;

  /**

  * 圖片處理

  * @author anEngineer

  *

  */

  public class DealPhoto {

  /**

  * 判斷照片類型 jpg png gif 目前只支持這三種格式

  * @param file

  * @return

  */

  public static boolean isPhoto(FormFile file) {

  String fileName = getString(filegetFileName());

  if (fileNameequals())

  return false;

  if ((fileNametoLowerCase()endsWith(jpg))

  || (fileNametoLowerCase()endsWith(gif))

  || (fileNametoLowerCase()endsWith(png)))

  return true;

  else

  return false;

  }

  /**

  *

  * @param str

  * @return

  */

  public static String getString(String str) {

  if (str == null)

  str = ;

  if (strequals(null))

  str = ;

  str = strtrim();

  return str;

  }

  /**

  * 判斷文件是否存在

  * @param fileName

  * @param dir

  * @return

  */

  public static boolean isFileExist(String fileName String dir) {

  File files = new File(dir + fileName);

  return (filesexists()) ? true : false;

  }

  /**

  * 重命名

  * @param fileName

  * @param dir

  */

  public static void rename(String fileName String dir) {

  String extendFile = ;

  if (isJpg(fileName))

  extendFile = jpg;

  else if (isGif(fileName))

  extendFile = gif;

  else if (isPng(fileName))

  extendFile = png;

  else

  extendFile = jpg;

  Random random = new Random();

  int add = randomnextInt();

  String ret = fileName + add + extendFile;

  while (isFileExist(ret dir)) {

  add = randomnextInt();

  ret = fileName + add + extendFile;

  }

  File file = new File(dir + fileName);

  File reFile = new File(dir + ret);

  filerenameTo(reFile);

  }

  public static boolean isGif(String file) {

  if (filetoLowerCase()endsWith(gif)) {

  return true;

  } else {

  return false;

  }

  }

  public static boolean isJpg(String file) {

  if (filetoLowerCase()endsWith(jpg)) {

  return true;

  } else {

  return false;

  }

  }

  public static boolean isPng(String file) {

  if (filetoLowerCase()endsWith(png)) {

  return true;

  } else {

  return false;

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/ky/201311/27880.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.