最近一个基于Struts的项目中要用到上传多张图片到系统中
我在网上找了一下
发现大多数都是转载的例子
对项目的作用不大
故写下了这个例子
希望对有用到的朋友有所帮助
要求JDK
以上
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中的标签库文件
一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;
}
}
}