Struts2_文件上传
2017-07-13 17:05
477 查看
人有一个特点,越是得不到的东西,越想得到,越是不能接触的东西就越想接触。反过来,越容易得到的东西越不知珍惜。
文件上传的方法很简单,注意几个点:
1.在前端界面,表单中加上
2.在execute()方法中,要加上
struts代码:
Action代码:
完成以上功能,只是将文件上传到一个临时的目录中,如果要将文件保存到指定目录,需要对临时目录中的文件进行操作,往往需要对临时目录的文件进行复制操作。
Action代码:
文件上传的方法很简单,注意几个点:
1.在前端界面,表单中加上
enctype="multipart/form-data"
2.在execute()方法中,要加上
FileUtils.copyFile(photo[j], destFile);,因为表单上传后会作为一个临时文件存储,所以要用前面的代码将临时文件复制到目标文件夹中。
1.单文件上传
前段页面代码:<%@ page language="java" pageEncoding="UTF-8" %> <%@ page contentType="text/html; charset=UTF-8" %> <!DOCTYPE html > <html> <head> <meta charset="UTF-8"> <title>上传界面</title> </head> <body> <form action="login.action" method="post" enctype="multipart/form-data"> 姓名:<input type="text" name="username"><br> 图片:<input type="file" name="photo"><br> <input type="submit" value="提交"> </form> </body> </html>
struts代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!-- 声明常量:关联自己写的proporties文件 --> <constant name="struts.custom.i18n.resources" value="message"/> <!-- 声明常量:配置文件上传的最大尺寸 --> <constant name="struts.multipart.maxSize" value="5242880"/> <package name="root" namespace="/" extends="struts-default"> <acti b88d on name="login" class="com.action.LoginAction"> <!-- 重写fileUpload拦截器 --> <interceptor-ref name="fileUpload"> <!-- 允许上传的文件格式 --> <param name="allowedTypes"> image/bmp,image/png,image/gif,image/pjpeg,image/jpg </param> <!-- 允许上传的文件最大尺寸 --> <param name="maximumSize">2097152</param> </interceptor-ref> <!-- 注意:一定加上默认的拦截器,当引用了自己写的拦截器后默认的就不生效了 --> <interceptor-ref name="defaultStack"/> <result>/success.jsp</result> </action> </package> </struts>
Action代码:
package com.action; import java.io.File; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; public class LoginAction { private String username; private File photo; private String photoFileName; private String photoContentType; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public File getPhoto() { return photo; } public void setPhoto(File photo) { this.photo = photo; } public String getPhotoFileName() { return photoFileName; } public void setPhotoFileName(String photoFileName) { this.photoFileName = photoFileName; } public String getPhotoContentType() { return photoContentType; } public void setPhotoContentType(String photoContentType) { this.photoContentType = photoContentType; } public String execute() throws Exception{ return "success"; } }
完成以上功能,只是将文件上传到一个临时的目录中,如果要将文件保存到指定目录,需要对临时目录中的文件进行操作,往往需要对临时目录的文件进行复制操作。
public String execute() throws Exception{ File destFile=new File(ServletActionContext.getServletContext().getRealPath("/upload/"+photoFileName)); FileUtils.copyFile(photo, destFile); return "success"; }
2.多文件上传
上传多个文件,只需将上例中与文件相关的属性改成数组格式,将上传的方法也改成操作数组即可。Action代码:
package com.action; import java.io.File; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; public class LoginAction { private String username; private File[] photo; private String[] photoFileName; private String[] photoContentType; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public File[] getPhoto() { return photo; } public void setPhoto(File[] photo) { this.photo = photo; } public String[] getPhotoFileName() { return photoFileName; } public void setPhotoFileName(String[] photoFileName) { this.photoFileName = photoFileName; } public String[] getPhotoContentType() { return photoContentType; } public void setPhotoContentType(String[] photoContentType) { this.photoContentType = photoContentType; } public String execute() throws Exception{ for (int j = 0; j < photo.length; j++) { System.out.println(ServletActionContext.getServletContext().getRealPath("/upload/"+photoFileName)); File destFile=new File(ServletActionContext.getServletContext().getRealPath("/upload/"+photoFileName[j])); FileUtils.copyFile(photo[j], destFile); } return "success"; } }
相关文章推荐
- Struts2文件上传在配置fileUpload拦截器中设置文件大小无效的问题
- 在Struts2中实现文件上传(一)
- struts2-上传文件(1)
- Struts2学习笔记14:Struts2的文件上传和下载【续】
- 在Struts2中实现文件上传
- 使用struts2中的文件上传
- Ext2.1 + Struts2 实现简单的文件上传
- 手工处理 Struts2 框架上传的文件
- Struts2中文件过大上传失败问题解决方案org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException
- 在Struts2中实现文件上传(二)
- struts2文件上传
- struts2文件上传
- Struts2 上传文件时取得文件扩展名的最好方法
- struts2文件上传的过滤
- Struts2文件的上传和下载
- 一篇关于struts2文件上传的好文章
- Struts2教程7:上传任意多个文件
- Struts2教程7:上传任意多个文件
- Struts2学习笔记13:Struts2的文件上传和下载
- struts2 common-fileupload上传文件大小限制