Struts2实现上传单个文件功能
2017-06-03 08:54
661 查看
upload.jsp 这个页面选择提交文件,提交到uploadImage.action
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form action="uploadImage.action" enctype="multipart/form-data" method="post"> please select the file:<input type="file" name="upload"> <input type="submit" value="上传文件"> </form> </body> </html>
FileUploadAction.java 将传来的file进行处理
package action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.commons.io.IOUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class FileUploadAction extends ActionSupport{ private File upload; private String uploadFileName; private String uploadContentType; public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public String execute(){ System.out.println(upload); System.out.println(uploadContentType); System.out.println(uploadFileName); String savePath = ServletActionContext.getServletContext().getRealPath("/upload/"+this.uploadFileName); System.out.println(savePath); try{ FileInputStream fis = new FileInputStream(upload); FileOutputStream fos = new FileOutputStream(savePath); IOUtils.copy(fis, fos); fos.flush(); fos.close(); fis.close(); }catch(Exception e){ e.printStackTrace(); } return "success"; } }
uploadFileName和uploadContentType,这两个属性分别用于封装上传文件的文件名、上传文件的文件类型
Struts.xml配置
<action name="uploadImage" class="action.FileUploadAction"> <result name="success">uploadSuccess.jsp</result> <result name="input">uploadError.jsp</result> </action>
成功失败界面随便写一个就行了,不贴了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- Struts2实现图片上传功能 (单个图片文件)
- struts2实现文件上传(单个、批量文件上传)
- Struts2实现单文件的上传功能实例详解及源代码
- struts2文件上传功能的实现
- 用Struts2实现文件上传时显示进度条功能
- Struts2:单个文件上传的两种实现方式
- Struts2实现文件上传功能
- 【SSH网上商城项目实战13】Struts2实现文件上传功能
- struts2实现文件批量上传功能
- 【SSH网上商城项目实战13】Struts2实现文件上传功能
- Spring boot 实现单个或批量文件上传功能
- struts2实现单个文件上传
- struts2实现上传文件(单个、多个)
- 【SSI开发总结.8】Struts2中实现文件上传功能
- 用Struts2更好的实现文件的上传、下载功能以及解决中文名称问题
- 使用html+ajax+formdata+SpringMVC实现单个文件,和多个文件上传功能
- Struts2-上传/下载文件功能实现
- Struts2实现文件上传时显示进度条功能
- Struts2实现文件上传功能实例解析
- Struts2中实现文件上传的功能