struts文件上传
来源:互联网 发布:直播看电视的软件 编辑:程序博客网 时间:2024/05/29 17:55
Struts2单文件上传
1创建文件file.jsp
<form action="uploadfile" enctype="multipart/form-data" method="post"><input type="file" name="myfile"/>
<input type="submit" value="提交"/>
</form>
2创建action
public class MyFileUpload extends ActionSupport{private String myfileFileName;
private File myfile;
private String myfileContentType;
public String upload() throws IOException{
//得到上传文件保存的路径
String savePath=ServletActionContext.getServletContext().getRealPath("/upload");
//上传后保存的文件
File outFile=new File(savePath+"/"+myfileFileName);
//上传文件操作
FileUtils.copyFile(myfile, outFile);
return "success";
}
//省略get/set方法
}
说明:上面的FileUtils.copyFile(face, outfile); 方法,使用Common-io.jar文件中的一个工具类。如果不使用该方法,可以自己实现文件的读写:
FileOutputStream fos = new FileOutputStream("D:/struts/"+myfileFileName);
FileInputStream fis = new FileInputStream(myfile);
byte[] b = new byte[1024];
int len = 0;
while((len = fis.read(b))!=-1){
fos.write(b,0,len);
}
fos.close();
fis.close();
3配置struts.xml文件
<action name="*file" class="base.MyFileUpload" method="{1}"><result>/success.jsp</result>
</action>
修改文件上传
增加文件类型的过滤,文件大小的过滤
修改action
public class MyFileUpload extends ActionSupport{
private String myfileFileName;
private File myfile;
private String myfileContentType;
private String msg;
private long allowSize;
private String allowType;
public String upload() throws IOException{
if(myfile!=null){
//判断文件大小
if(isAllowSize()){
//判断文件类型
if(isAllowType()){
String savePath=ServletActionContext.getServletContext().getRealPath("/upload");
File outFile=new File(savePath+"/"+myfileFileName);
FileUtils.copyFile(myfile, outFile);
return "success";
}else{
msg="文件类型不符";
return "error";
}
}else{
msg="文件过大";
return "error";
}
}
return "success";
}
//判断大小是否符合要求
public boolean isAllowSize(){
if(myfile!=null){
long size=myfile.length();
System.out.println(size);
if(size>allowSize){
return false;
}else{
return true;
}
}
return false;
}
//判断文件类型是否符合要求
public boolean isAllowType(){
String fileType=myfileFileName.substring(myfileFileName.indexOf(".")+1);
String[] type=allowType.split(",");
for(int i=0;i<type.length;i++){
if(fileType.equals(type[i])){
return true;
}
}
return false;
}
//省略get/set方法
}
修改struts.xml
<action name="*file" class="base.MyFileUpload" method="{1}">
<param name="allowType">jpg,zip,text,gif</param>
<param name="allowSize">1048000</param>
<result name="error">/file.jsp</result>
<result>/success.jsp</result>
</action>
注意:文件上传一定要完成:a、文件类型的过滤;b、文件大小的过滤。在Struts中,可以用struts自带的拦截器来完成。
整个表单的总文件的大小,不能超过struts.properties里限定的大小,如:
struts.multipart.maxSize=10971520
过滤文件的大小,类型如:
<interceptor-ref name="fileUpload">
<param name="maximumSize">4194304</param>
<!-- 配置允许上传文件的类型,如果有多个类型用","隔开 -->
<param name="fileUpload.allowedTypes">application/vnd.ms-excel,text/plain</param>
<!--配置允许上传文件的扩展名,如果有多个用","隔开 -->
<param name="fileUpload.allowedExtensions">txt,excel,ppt</param>
</interceptor-ref>
多文件上传
public class MyFileUpload extends ActionSupport{private String[] myfileFileName;
private File[] myfile;
private String[] myfileContentType;
public String upload() throws IOException{
String savePath=ServletActionContext.getServletContext().getRealPath("/upload");
//确保有上传文件
if(myfile!=null&&myfile.length>0){
for(int i=0;i<myfile.length;i++){
File outFile=new File(savePath+"/"+myfileFileName[i]);
if(myfile[i]!=null&&myfile[i].length()>0){
FileUtils.copyFile(myfile[i], outFile);
}else{
System.out.println("上传有误");
}
}
}
return "success";
}
//省略
}
默认的错误信息
在struts2-core-2.x.x.x.jar\org\apache\struts2\struts-messages.properties文件中定义:
struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=File too large: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3}
{0}:<input type=“file” name=“uploadImage”>中name属性的值
{1}:上传文件的名称
{2}:上传文件保存到临时目录的名称
{3}:上传文件的类型(对struts.messages.error.file.too.large是上传文件的大小)
我们可以在Action的统计目录下创建一个fileuploadmessage.properties文件, 文件名没有要求, 但必须是properties文件, 在其中输入:
struts.messages.error.uploading=上传错误: {0}
struts.messages.error.file.too.large=文件太大: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=不支持的文件类型: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=不支持的文件扩展名: {0} "{1}" "{2}" {3}
<constant name="struts.custom.i18n.resources" value="cn.xing.upload.fileuploadmessage">
</constant>
阅读全文
0 0
- Struts的文件上传
- struts文件上传要点
- Struts的文件上传
- struts文件上传要点
- Struts的文件上传
- Struts的文件上传
- Struts的文件上传
- Struts上传文件笔记
- Struts文件上传
- Struts的文件上传
- 上传文件(struts篇)
- Struts的文件上传
- struts 多文件上传
- struts构建文件上传
- struts多文件上传
- Struts的文件上传
- 使用Struts上传文件
- struts文件上传
- Hibernate注解、类级别注解
- 间接寻址的基本操作实现
- Project Euler Problem 25 :1000-digit Fibonacci number
- 启动hive 遇到jline 版本冲突问题解决
- duobuju
- struts文件上传
- 侧拉 DrawerLayout
- Hadoop学习笔记(三)漫画解读HDFS读写原理
- 用c语言求解一元二次方程
- 注册表单验证的模块
- AngularJs购物车代码。
- Codeforces Round #440 (A-C ) 题解
- Unix Shell编程——有序输出参数列表
- Android仿博雅斗地主