首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

Struts下传多个及N个文件的例子

2012-08-29 
Struts上传多个及N个文件的例子一。web.xml文件??二。struts-config.xml文件??三。upload.jsp文件?%@ page l

Struts上传多个及N个文件的例子

一。web.xml文件

?Struts下传多个及N个文件的例子Struts下传多个及N个文件的例子

    ?二。struts-config.xml文件

    ?Struts下传多个及N个文件的例子Struts下传多个及N个文件的例子

      ?三。upload.jsp文件

      ?Struts下传多个及N个文件的例子Struts下传多个及N个文件的例子
        <%@ page language="java" import="java.util.*" pageEncoding="gb2312" errorPage="error.jsp"%><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%><%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%><%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%><%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="title"%><!-- 上传多个文件的jsp文件 //可以随便转载,请保留出自http://www.javaresearch.org 作者anEngineer--><html><head><title>上传多个文件的jsp文件</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="struts,多个文件,上传"><meta http-equiv="description" content="This is a test"></head><body><html:form action="uploaded.do" enctype="multipart/form-data" method="post"> <table border="1" width="80%" align="center"> <tr bgColor=#62bcff> <td align="left">文件类型</td> <td align="left">文件描述</td> <td align="left">文件名</td> </tr> <tr> <td align="left"><select name="type"> <option value="1" selected>湖南省</option> <option value="2">湖北省</option> <option value="3">广东省</option> <option value="4">北京市</option> <option value="5">上海市</option> </select></td> <td align="left"><input type="text" name="name" value=""></td> <td align="left"><input type="file" name="file0" size="35" value=""> </td> </tr> <tr> <td align="left"><select name="type"> <option value="1"selected>湖南省</option> <option value="2">湖北省</option> <option value="3">广东省</option> <option value="4">北京市</option> <option value="5">上海市</option> </select></td> <td align="left"><input type="text" name="name" value=""></td> <td align="left"><input type="file" name="file1" size="35" value=""> </td> </tr> <tr> <td align="left"><select name="type"> <option value="1">湖南省</option> <option value="2"selected>湖北省</option> <option value="3">广东省</option> <option value="4">北京市</option> <option value="5">上海市</option> </select></td> <td align="left"><input type="text" name="name" value=""></td> <td align="left"><input type="file" name="file2" size="35" value=""> </td> </tr> <tr> <td align="left"><select name="type"> <option value="1">湖南省</option> <option value="2"selected>湖北省</option> <option value="3">广东省</option> <option value="4">北京市</option> <option value="5">上海市</option> </select></td> <td align="left"><input type="text" name="name" value=""></td> <td align="left"><input type="file" name="file3" size="35" value=""> </td> </tr> <tr> <td align="left"><select name="type"> <option value="1">湖南省</option> <option value="2">湖北省</option> <option value="3"selected>广东省</option> <option value="4">北京市</option> <option value="5">上海市</option> </select></td> <td align="left"><input type="text" name="name" value=""></td> <td align="left"><input type="file" name="file4" size="35" value=""> </td> </tr> <tr> <td align="left"><select name="type"> <option value="1">湖南省</option> <option value="2">湖北省</option> <option value="3"selected>广东省</option> <option value="4">北京市</option> <option value="5">上海市</option> </select></td> <td align="left"><input type="text" name="name" value=""></td> <td align="left"><input type="file" name="file5" size="35" value=""> </td> </tr> <tr> <td align="left"><select name="type"> <option value="1">湖南省</option> <option value="2">湖北省</option> <option value="3">广东省</option> <option value="4"selected>北京市</option> <option value="5">上海市</option> </select></td> <td align="left"><input type="text" name="name" value=""></td> <td align="left"><input type="file" name="file6" size="35" value=""> </td> </tr> <tr> <td align="left"><select name="type"> <option value="1">湖南省</option> <option value="2">湖北省</option> <option value="3">广东省</option> <option value="4"selected>北京市</option> <option value="5">上海市</option> </select></td> <td align="left"><input type="text" name="name" value=""></td> <td align="left"><input type="file" name="file7" size="35" value=""> </td> </tr> <tr> <td align="left"><select name="type"> <option value="1">湖南省</option> <option value="2">湖北省</option> <option value="3">广东省</option> <option value="4">北京市</option> <option value="5"selected>上海市</option> </select></td> <td align="left"><input type="text" name="name" value=""></td> <td align="left"><input type="file" name="file8" size="35" value=""> </td> </tr> <tr> <td align="left"><select name="type"> <option value="1">湖南省</option> <option value="2">湖北省</option> <option value="3">广东省</option> <option value="4">北京市</option> <option value="5"selected>上海市</option> </select></td> <td align="left"><input type="text" name="name" value=""></td> <td align="left"><input type="file" name="file9" size="35" 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文件index.jsp,error.jsp可以自己写。

        五。action文件

        ?Struts下传多个及N个文件的例子Struts下传多个及N个文件的例子
          package com.fangchuang.action;/** * @author anEngineer 处理上传文件action */public class UploadAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { //可以随便转载,请保留出自http://www.javaresearch.org 作者anEngineer DynaActionForm fileForm = (DynaActionForm) form; String[] types = (String[]) fileForm.get("type"); String[] names = (String[]) fileForm.get("name"); FormFile file0 = (FormFile) fileForm.get("file0"); FormFile file1 = (FormFile) fileForm.get("file1"); FormFile file2 = (FormFile) fileForm.get("file2"); FormFile file3 = (FormFile) fileForm.get("file3"); FormFile file4 = (FormFile) fileForm.get("file4"); FormFile file5 = (FormFile) fileForm.get("file5"); FormFile file6 = (FormFile) fileForm.get("file6"); FormFile file7 = (FormFile) fileForm.get("file7"); FormFile file8 = (FormFile) fileForm.get("file8"); FormFile file9 = (FormFile) fileForm.get("file9"); Map<String, FormFile> fileMap = new HashMap<String, FormFile>(); fileMap.put("file0" + "*" + types[0] + "*" + names[0], file0); fileMap.put("file1" + "*" + types[1] + "*" + names[1], file1); fileMap.put("file2" + "*" + types[2] + "*" + names[2], file2); fileMap.put("file3" + "*" + types[3] + "*" + names[3], file3); fileMap.put("file4" + "*" + types[4] + "*" + names[4], file4); fileMap.put("file5" + "*" + types[5] + "*" + names[5], file5); fileMap.put("file6" + "*" + types[6] + "*" + names[6], file6); fileMap.put("file7" + "*" + types[7] + "*" + names[7], file7); fileMap.put("file8" + "*" + types[8] + "*" + names[8], file8); fileMap.put("file9" + "*" + types[9] + "*" + names[9], file9); Set fileSet = fileMap.entrySet(); Iterator iter = fileSet.iterator(); // 取当前系统路径E:\Tomcat5\webapps\strutsUpload\ 其中strutsUpload为当前context String filePath = this.getServlet().getServletContext().getRealPath("/"); // 保存文件的文件夹 File savePath = new File(filePath + "UploadFiles\"); filePath = filePath+ "UploadFiles\"; if (!savePath.exists()) { savePath.mkdir(); } while (iter.hasNext()) { Map.Entry unit = (Map.Entry) iter.next(); String key = (String) unit.getKey(); FormFile file = (FormFile) unit.getValue(); //文件大小符合要求,且是图片文件 if ((file.getFileSize() >= 1)&& DealPhoto.isPhoto(file)) { //图片类别 String photoType = key.substring(key.indexOf("*") + 1, key .lastIndexOf("*")); //图片描述 String photoName = key.substring(key.lastIndexOf("*") + 1, key .length()); //存数据库操作,在数据库中保存文件的名称,类型,及在服务器上的相对路径 // //判断是否重名 if(DealPhoto.isFileExist(file.getFileName(),filePath)) DealPhoto.rename(file.getFileName(),filePath); try { InputStream stream = file.getInputStream();// 把文件读入 // 建立一个上传文件的输出流 OutputStream bos = new FileOutputStream(filePath+file.getFileName()); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, bytesRead);// 将文件写入服务器 } bos.close(); stream.close(); } catch (Exception e) { e.printStackTrace(); } } } return mapping.findForward("uploaded"); //还有其他可以改正的地方,如错误信息提示,把照片处理函数放到一个公用类,写文件操作等 //可以随便转载,请保留出自http://www.javaresearch.org 作者anEngineer }}

          ?六。照片处理类文件

          ?Struts下传多个及N个文件的例子Struts下传多个及N个文件的例子
            package?com.fangchuang.run; ??/** ??*?图片处理 ??*?@author?anEngineer ??* ??*/??public?class?DealPhoto?{ ??????/** ??????*?判断照片类型?.jpg?.png?.gif?目前只支持这三种格式 ??????*?@param?file ??????*?@return ??????*/??????public?static?boolean?isPhoto(FormFile?file)?{ ??????????String?fileName?=?getString(file.getFileName()); ??????????if?(fileName.equals("")) ??????????????return?false; ??????????if?((fileName.toLowerCase().endsWith(".jpg")) ??????????????????||?(fileName.toLowerCase().endsWith(".gif")) ??????????????????||?(fileName.toLowerCase().endsWith(".png"))) ??????????????return?true; ??????????else??????????????return?false; ??????} ????????/** ??????*? ??????*?@param?str ??????*?@return ??????*/??????public?static?String?getString(String?str)?{ ??????????if?(str?==?null) ??????????????str?=?""; ??????????if?(str.equals("null")) ??????????????str?=?""; ??????????str?=?str.trim(); ??????????return?str; ??????} ????????/** ??????*?判断文件是否存在 ??????*?@param?fileName ??????*?@param?dir ??????*?@return ??????*/??????public?static?boolean?isFileExist(String?fileName,?String?dir)?{ ??????????File?files?=?new?File(dir?+?fileName); ??????????return?(files.exists())???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?=?random.nextInt(10000); ??????????String?ret?=?fileName?+?add?+?extendFile; ??????????while?(isFileExist(ret,?dir))?{ ??????????????add?=?random.nextInt(10000); ??????????????ret?=?fileName?+?add?+?extendFile; ??????????} ??????????File?file?=?new?File(dir?+?fileName); ??????????File?reFile?=?new?File(dir?+?ret); ??????????file.renameTo(reFile); ??????} ????????public?static?boolean?isGif(String?file)?{ ??????????if?(file.toLowerCase().endsWith(".gif"))?{ ??????????????return?true; ??????????}?else?{ ??????????????return?false; ??????????} ??????} ????????public?static?boolean?isJpg(String?file)?{ ??????????if?(file.toLowerCase().endsWith(".jpg"))?{ ??????????????return?true; ??????????}?else?{ ??????????????return?false; ??????????} ??????} ????????public?static?boolean?isPng(String?file)?{ ??????????if?(file.toLowerCase().endsWith(".png"))?{ ??????????????return?true; ??????????}?else?{ ??????????????return?false; ??????????} ??????} ??}?

热点排行