如何用struts上传文件,并限制文件扩展名和文件大小.
2008-02-29 20:43
387 查看
在做开发时难免会碰到上传文件,近来我就来和大家分享一下心得,供大家讨论.多提宝贵意见.
在struts中上传文件当然要用struts的标签了,<html:file>.用该标签是需要注意以下几点:
<html:file> 该标签必须和,<html:form>一起使用并且必须嵌套在<html:form>中,其实struts的html标签必须要写在<html:form>中.
<html:form>中的method属性必须是post.
<html:form>的enctype属性必须是multipart/form-data
<html:file>必须设置property属性,并且该属性的值必须和ActionFormBean 中的 org.apache.struts.upload.FormFile类型属性对应.
upload.jsp代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ page language="java" pageEncoding="utf-8"%>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
prefix="bean"%>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-html"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
prefix="html"%>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
prefix="logic"%>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-tiles"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
prefix="tiles"%>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-template"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
prefix="template"%>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-nested"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
prefix="nested"%>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html:html locale="true">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html:base />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>upload.jsp</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="pragma" content="no-cache">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="cache-control" content="no-cache">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="expires" content="0">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="description" content="This is my page">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<link rel="stylesheet" type="text/css" href="styles.css">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<h3>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
上传文件测试
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</h3>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html:form action="upload.do" method="post" enctype="multipart/form-data">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html:file property="file"></html:file><br>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html:submit></html:submit>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</html:form>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</html:html>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
(upload.jsp)
大家注意upload.jsp中<html:file> 的property属性.该值和actionfrom的属性相同,HtmlfileForm代码如下
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Generated by MyEclipse Struts
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Template path: templates/java/JavaClass.vtl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package com.uploadtest.struts.form;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.action.ActionErrors;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.action.ActionForm;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.action.ActionMapping;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.upload.FormFile;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* MyEclipse Struts Creation date: 02-29-2008
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* XDoclet definition:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @struts.form name="htmlfileForm"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class HtmlfileForm extends ActionForm ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Generated Methods
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private FormFile file;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public FormFile getFile() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return file;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setFile(FormFile file) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.file = file;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Method validate
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param mapping
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param request
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return ActionErrors
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public ActionErrors validate(ActionMapping mapping,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
HttpServletRequest request) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Method reset
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param mapping
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param request
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void reset(ActionMapping mapping, HttpServletRequest request) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
(HtmlfileForm)
然后就是最主要的处理代码部分代码如下(UploadAction),在该Action中做了很多处理,都有注释,自己看.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Generated by MyEclipse Struts
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Template path: templates/java/JavaClass.vtl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package com.uploadtest.struts.action;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileOutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.InputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.OutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.ArrayList;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.List;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.action.Action;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.action.ActionForm;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.action.ActionForward;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.action.ActionMapping;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.action.ActionMessage;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.upload.FormFile;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.uploadtest.struts.form.HtmlfileForm;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* MyEclipse Struts Creation date: 02-29-2008
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* XDoclet definition:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @struts.action path="/upload" name="htmlfileForm" input="/upload.jsp"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* scope="request" validate="true"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class UploadAction extends Action ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Generated Methods
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Method execute
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param mapping
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param form
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param request
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param response
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return ActionForward
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public ActionForward execute(ActionMapping mapping, ActionForm form,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
HttpServletRequest request, HttpServletResponse response) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// webapps/uploadtest/upload
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String dir = servlet.getServletContext().getRealPath("/upload");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List<String> list = new ArrayList<String>();//注意文件类型最好全部用小写
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list.add("jpg");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list.add("jpeg");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list.add("gif");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list.add("bmp");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 允许上传的文件类型列表可以写在配置文件中,通过xml的解析获得。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!dir.endsWith("/"))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dir = dir.concat("/");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HtmlfileForm htmlfileForm = (HtmlfileForm) form;// TODO Auto-generated
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FormFile file = htmlfileForm.getFile();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (file == null) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return mapping.getInputForward();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String fname = file.getFileName();// 获取文件名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int fsize = file.getFileSize();// 获取文件大小
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String ext = fname.substring(fname.lastIndexOf(".") + 1, fname.length());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获取文件类型,即扩展名,通过String类的substring方法截取字符串,lastIndexOf获取某个字符串最后出现的索引。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ext = ext.toLowerCase();// 全部转换成小写。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (!list.contains(ext)) ...{// 判断该类型是否为允许上传的文件类型
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("不支持该文件类型上传,该文件类型是:" + ext);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 可以在此构建ActionMessage对象并返回页面显示错误
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return mapping.getInputForward();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (fsize > 1024 * 1024) ...{// 判断文件大小是否为允许上传的大小。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 可以在此构建ActionMessage对象并返回页面显示错误
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("文件太大");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return mapping.getInputForward();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InputStream in = null;// 输入流用来读取用户上传文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
OutputStream out = null;// 用来将用户上传文件存出在服务器特定目录中。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
in = file.getInputStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out = new FileOutputStream(dir + fname);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int byteread = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] bytes = new byte[8192];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while ((byteread = in.read(bytes, 0, 8192)) != -1) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.write(bytes, 0, byteread);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(e.getMessage());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return mapping.getInputForward();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} finally ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
in.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(e.getMessage());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return mapping.getInputForward();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("chenggong");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return mapping.findForward("suc");//suc只是一个成功跳转的设置.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
(UploadAction)
以下是struts的配置文件.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<struts-config>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<data-sources />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form-beans>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form-bean name="htmlfileForm"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
type="com.uploadtest.struts.form.HtmlfileForm" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</form-beans>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<global-exceptions />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<global-forwards />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<action-mappings>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<action attribute="htmlfileForm" input="/upload.jsp"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
name="htmlfileForm" path="/upload" scope="request"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
type="com.uploadtest.struts.action.UploadAction">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<forward name="suc" path="/suc.jsp" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</action>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</action-mappings>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<controller processorClass="com.uploadtest.struts.util.CharSet"></controller>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<message-resources
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
parameter="com.uploadtest.struts.ApplicationResources" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</struts-config>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
以上是个人的一点愚见,如有不对,请指正.
在struts中上传文件当然要用struts的标签了,<html:file>.用该标签是需要注意以下几点:
<html:file> 该标签必须和,<html:form>一起使用并且必须嵌套在<html:form>中,其实struts的html标签必须要写在<html:form>中.
<html:form>中的method属性必须是post.
<html:form>的enctype属性必须是multipart/form-data
<html:file>必须设置property属性,并且该属性的值必须和ActionFormBean 中的 org.apache.struts.upload.FormFile类型属性对应.
upload.jsp代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ page language="java" pageEncoding="utf-8"%>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
prefix="bean"%>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-html"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
prefix="html"%>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
prefix="logic"%>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-tiles"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
prefix="tiles"%>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-template"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
prefix="template"%>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ taglib uri="http://jakarta.apache.org/struts/tags-nested"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
prefix="nested"%>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html:html locale="true">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html:base />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>upload.jsp</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="pragma" content="no-cache">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="cache-control" content="no-cache">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="expires" content="0">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="description" content="This is my page">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<link rel="stylesheet" type="text/css" href="styles.css">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<h3>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
上传文件测试
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</h3>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html:form action="upload.do" method="post" enctype="multipart/form-data">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html:file property="file"></html:file><br>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html:submit></html:submit>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</html:form>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</html:html>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
(upload.jsp)
大家注意upload.jsp中<html:file> 的property属性.该值和actionfrom的属性相同,HtmlfileForm代码如下
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Generated by MyEclipse Struts
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Template path: templates/java/JavaClass.vtl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package com.uploadtest.struts.form;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.action.ActionErrors;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.action.ActionForm;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.action.ActionMapping;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.upload.FormFile;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* MyEclipse Struts Creation date: 02-29-2008
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* XDoclet definition:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @struts.form name="htmlfileForm"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class HtmlfileForm extends ActionForm ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Generated Methods
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private FormFile file;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public FormFile getFile() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return file;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setFile(FormFile file) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.file = file;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Method validate
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param mapping
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param request
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return ActionErrors
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public ActionErrors validate(ActionMapping mapping,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
HttpServletRequest request) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Method reset
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param mapping
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param request
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void reset(ActionMapping mapping, HttpServletRequest request) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
(HtmlfileForm)
然后就是最主要的处理代码部分代码如下(UploadAction),在该Action中做了很多处理,都有注释,自己看.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Generated by MyEclipse Struts
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Template path: templates/java/JavaClass.vtl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package com.uploadtest.struts.action;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileOutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.InputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.OutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.ArrayList;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.List;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.action.Action;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.action.ActionForm;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.action.ActionForward;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.action.ActionMapping;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.action.ActionMessage;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.struts.upload.FormFile;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.uploadtest.struts.form.HtmlfileForm;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* MyEclipse Struts Creation date: 02-29-2008
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* XDoclet definition:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @struts.action path="/upload" name="htmlfileForm" input="/upload.jsp"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* scope="request" validate="true"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class UploadAction extends Action ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Generated Methods
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Method execute
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param mapping
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param form
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param request
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param response
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return ActionForward
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public ActionForward execute(ActionMapping mapping, ActionForm form,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
HttpServletRequest request, HttpServletResponse response) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// webapps/uploadtest/upload
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String dir = servlet.getServletContext().getRealPath("/upload");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List<String> list = new ArrayList<String>();//注意文件类型最好全部用小写
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list.add("jpg");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list.add("jpeg");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list.add("gif");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list.add("bmp");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 允许上传的文件类型列表可以写在配置文件中,通过xml的解析获得。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!dir.endsWith("/"))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dir = dir.concat("/");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HtmlfileForm htmlfileForm = (HtmlfileForm) form;// TODO Auto-generated
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FormFile file = htmlfileForm.getFile();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (file == null) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return mapping.getInputForward();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String fname = file.getFileName();// 获取文件名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int fsize = file.getFileSize();// 获取文件大小
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String ext = fname.substring(fname.lastIndexOf(".") + 1, fname.length());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获取文件类型,即扩展名,通过String类的substring方法截取字符串,lastIndexOf获取某个字符串最后出现的索引。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ext = ext.toLowerCase();// 全部转换成小写。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (!list.contains(ext)) ...{// 判断该类型是否为允许上传的文件类型
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("不支持该文件类型上传,该文件类型是:" + ext);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 可以在此构建ActionMessage对象并返回页面显示错误
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return mapping.getInputForward();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (fsize > 1024 * 1024) ...{// 判断文件大小是否为允许上传的大小。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 可以在此构建ActionMessage对象并返回页面显示错误
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("文件太大");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return mapping.getInputForward();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InputStream in = null;// 输入流用来读取用户上传文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
OutputStream out = null;// 用来将用户上传文件存出在服务器特定目录中。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
in = file.getInputStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out = new FileOutputStream(dir + fname);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int byteread = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] bytes = new byte[8192];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while ((byteread = in.read(bytes, 0, 8192)) != -1) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.write(bytes, 0, byteread);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(e.getMessage());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return mapping.getInputForward();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} finally ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
in.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(e.getMessage());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return mapping.getInputForward();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("chenggong");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return mapping.findForward("suc");//suc只是一个成功跳转的设置.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
(UploadAction)
以下是struts的配置文件.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<struts-config>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<data-sources />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form-beans>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form-bean name="htmlfileForm"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
type="com.uploadtest.struts.form.HtmlfileForm" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</form-beans>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<global-exceptions />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<global-forwards />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<action-mappings>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<action attribute="htmlfileForm" input="/upload.jsp"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
name="htmlfileForm" path="/upload" scope="request"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
type="com.uploadtest.struts.action.UploadAction">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<forward name="suc" path="/suc.jsp" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</action>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</action-mappings>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<controller processorClass="com.uploadtest.struts.util.CharSet"></controller>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<message-resources
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
parameter="com.uploadtest.struts.ApplicationResources" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</struts-config>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
以上是个人的一点愚见,如有不对,请指正.
相关文章推荐
- 如何用struts上传文件,并限制文件扩展名和文件大小.
- ASP.net 如何限制上传文件的大小
- 如何解决ASP.NET中的文件上传大小限制的问题
- 如何限制asp.net中上传文件的大小的代码
- 如何解决IIS7上传文件大小限制,.NET 上传文件后 找不到目录解决
- 详细描述PHP处理web上传文件的流程。如何限制上传文件的大小不能超过某个数值?
- 如何修改上传文件大小及类型的限制
- php中web上传文件的原理及如何限制上传文件的大小及格式
- 如何在web.config文件中限制上传文件大小
- 如何修改PHP上传文件大小的限制
- 在使用struts框架时,如何改变上传文件总字节数限制大小
- 如何解决struts2中上传文件大小限制的问题
- 如何修改WAMP数据库上传文件的大小及上传时间限制
- 如何在wordpress上增加文件上传的大小限制(多种实现方法)
- spring MVC中CommonsMultipartResolver上传文件大小限制,如何对特定请求进行单独设置?
- 如何自定义上传文件大小限制
- 叫你如何修改Nginx与PHP的文件上传大小限制
- 如何修改Moodle上传文件大小的限制
- jQuery-File-Upload如何限制上传文件的大小
- 使用Struts实现文件上传,格式限制,大小限制