- 浏览: 397425 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (268)
- java (22)
- Acegi (8)
- Ajax (17)
- Annotation (3)
- Ant (3)
- JBOSS (6)
- Xdoclet (1)
- CSS (20)
- Data Warehouse (11)
- DB2 (3)
- DOM (1)
- dos (2)
- JMF (1)
- JMS (5)
- J2EE (17)
- Hibernate (7)
- struts (10)
- CORBA (1)
- 职业 (2)
- JSF (1)
- JSTL (8)
- 其它 (1)
- Log4j (7)
- svg (7)
- quartz (3)
- web2.0 (2)
- velocity (2)
- apache commons (1)
- js (9)
- html (4)
- sql (3)
- linux (4)
- dwr (14)
- spring (5)
- GWT (7)
- portlet (4)
- 软件工程 (10)
- actionscript (1)
- 测试 (1)
- tomcat (3)
- flash (0)
- 线程 (1)
- mysql (6)
- flex (1)
- oracle (7)
- crystalreport (4)
- itext (4)
- memcache (2)
- linux 监控 (2)
- mongodb (1)
- Kafka (5)
- 网络 (2)
- 分布式计算 (2)
最新评论
-
chenyongxin:
mark
JBoss 4.0.2集群基本知识及配置方法指南 -
softor:
我找到了,下载吧:http://ishare.iask.sin ...
jad是最简单的class反编译为java文件的小工具 (转载) -
softor:
求下载
dodo@lovehang.com
jad是最简单的class反编译为java文件的小工具 (转载) -
juedui0769:
不错!
请问: 如何在 将 log4j.appender ...
Tomcat 日志 配置 (转载) -
spp_1987:
// 建立一个上传文件的输出流
...
Struts上传多个及N个文件的例子
最近一个基于Struts的项目中要用到上传多张图片到系统中,我在网上找了一下,发现大多数都是转载的例子,对项目的作用不大,故写下了这个例子,希望对有用到的朋友有所帮助。要求JDK1.5以上,lib库要包含如下jar文件:antlr.jar,commons-beanutils.jar,commons-collections.jar,commons-digester.jar,commons-fileupload.jar,commons-logging.jar,commons-validator.jar,jakarta-oro.jar,struts.jar这些都是Struts中的jar文件以及Struts中的标签库文件。
//可以转载,请保留出自http://www.javaresearch.org/,作者anEngineer
一。web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>actionServlet</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<!-- struts actionServlet mapping-->
<servlet-mapping>
<servlet-name>actionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- welcome file list -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- error handle -->
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>
二。struts-config.xml文件
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- form beans -->
<form-beans>
<form-bean name="uploadForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="type" type ="java.lang.String[]"/>
<form-property name="name" type ="java.lang.String[]"/>
<form-property name="file0" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file1" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file2" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file3" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file4" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file5" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file6" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file7" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file8" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file9" type ="org.apache.struts.upload.FormFile"/>
</form-bean>
</form-beans>
<!-- forward -->
<global-forwards>
<forward name= "successed" path="/index.jsp"></forward>
<forward name= "failed" path="/error.jsp"></forward>
</global-forwards>
<!-- action handle-->
<action-mappings>
<action path="/uploaded" type="com.fangchuang.action.UploadAction"
name="uploadForm" scope="request" input="/upload.jsp">
<forward name="uploaded" path="/upload.jsp"></forward>
</action>
</action-mappings>
</struts-config>
三。upload.jsp文件
<%@ 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文件
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
}
}
六。照片处理类文件
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;
}
}
}
还有许多可以完善的地方,以后再补充!
//可以转载,请保留出自http://www.javaresearch.org/,作者anEngineer
一。web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>actionServlet</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<!-- struts actionServlet mapping-->
<servlet-mapping>
<servlet-name>actionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- welcome file list -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- error handle -->
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>
二。struts-config.xml文件
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- form beans -->
<form-beans>
<form-bean name="uploadForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="type" type ="java.lang.String[]"/>
<form-property name="name" type ="java.lang.String[]"/>
<form-property name="file0" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file1" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file2" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file3" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file4" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file5" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file6" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file7" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file8" type ="org.apache.struts.upload.FormFile"/>
<form-property name="file9" type ="org.apache.struts.upload.FormFile"/>
</form-bean>
</form-beans>
<!-- forward -->
<global-forwards>
<forward name= "successed" path="/index.jsp"></forward>
<forward name= "failed" path="/error.jsp"></forward>
</global-forwards>
<!-- action handle-->
<action-mappings>
<action path="/uploaded" type="com.fangchuang.action.UploadAction"
name="uploadForm" scope="request" input="/upload.jsp">
<forward name="uploaded" path="/upload.jsp"></forward>
</action>
</action-mappings>
</struts-config>
三。upload.jsp文件
<%@ 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文件
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
}
}
六。照片处理类文件
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;
}
}
}
还有许多可以完善的地方,以后再补充!
评论
1 楼
spp_1987
2010-07-05
// 建立一个上传文件的输出流
OutputStream bos = new FileOutputStream(filePath+file.getFileName());
如果 我要在服务器端 不保存上传的图片,只是把那些 图片读成二进制流 写入到数据库里面 该如何操作...........
OutputStream bos = new FileOutputStream(filePath+file.getFileName());
如果 我要在服务器端 不保存上传的图片,只是把那些 图片读成二进制流 写入到数据库里面 该如何操作...........
发表评论
-
使用Struts的Token机制解决表单的重复提交
2008-07-31 13:29 687Struts的Token(令牌)机制能够很好的解决表单重复提交 ... -
面对几个错误的解决关键
2007-10-28 12:45 89011、 org.springframework.orm.Obje ... -
Struts标签中的多层logic:iterator详解
2007-10-28 12:44 1244logic:Iterator标签(以 ... -
Struts标签
2007-10-28 12:44 861html:form注意事项 在使用html:form时需指定 ... -
Struts标签-Html(转载)
2007-10-28 12:43 1146Struts Html标签和标准的HTML标签功能相同,这里提 ... -
Struts标签
2007-10-28 12:43 1032Struts标签 solo L 发布日 ... -
Strus常见错误及原因分析
2007-10-28 12:42 3041本篇文章包含了在用Struts开发web应用时经常碰到的一些异 ... -
Struts提供了五个标签库
2007-10-28 12:41 1233Struts提供了五个标签库,即:HTML、Bean、Logi ... -
Struts常见错误及原因分析
2007-10-28 12:41 7021 异常 javax.servlet.jsp.JspE ...
相关推荐
Struts2多个文件上传 多个文件上传分为List集合和数组,下面我们着重介绍一下list集合的上传。都大同小异。
实现struts同时上传多个文件!
用struts上传多个文件的方法,建立上传用的JSP文件配置struts-config.xml文件
struts2实现的多个文件上传struts2实现的多个文件上传struts2实现的多个文件上传struts2实现的多个文件上传
struts1上传多个文件 jquery multi什么的组件
struts例子struts例子struts例子struts例子struts例子struts例子struts例子struts例子struts例子struts例子struts例子struts例子struts例子struts例子struts例子
需求 1.能够对多个文件进行上传(可以选择上传文件个数,也即上传文件个数不定) 2.能够对上传路径进行配置文件指定(upload.properties),使用...多文件 上传 下载 随意文件 java Struts2 单例 配置 动态读取 李顺利
struts上传struts上传 struts上传struts上传
struts1.2开发,可实习自定义,任意多个文件上传
最近一个基于Struts的项目中要用到上传多张图片到系统中,我在网上找了一下,发现大多数都是转载的例子,对项目的作用不大,故写下了这个例子,希望对有用到的朋友有所帮助。要求JDK1.5以上,lib库要包含如下jar文件...
Struts2实现单个文件 多个文件 上传与下载 多个拦截器 直接导入MyEclipse包中即可使用。
struts2多文件上传例子及单文件上传例子
在struts2基础上实现的包括单文件、多文件的上传及下载
struts文件上传.pdf 文件上传struts文件上传.pdf 文件上传struts文件上传.pdf 文件上传struts文件上传.pdf 文件上传
struts1单个上传和多个上传,一个工程,两张上传方式
JavaEE Struts文件上传
struts2实现文件上传(单个+多个文件上传
struts2文件上传例子(支持大文件)
struts1.x实现多文件上传struts1.x实现多文件上传struts1.x实现多文件上传struts1.x实现多文件上传struts1.x实现多文件上传struts1.x实现多文件上传
struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传