struts2文件上传
来源:互联网 发布:淘宝要怎么运营 编辑:程序博客网 时间:2024/06/07 20:10
FileUpload.java:
package blog.action;import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;public class FileUpload {private File fileUpload;//得到上传的文件private String fileUploadContentType;//得到文件的类型private String fileUploadFileName;//得到文件的名称private String msg;public File getFileUpload() {return fileUpload;}public void setFileUpload(File fileUpload) {this.fileUpload = fileUpload;}public String getFileUploadContentType() {return fileUploadContentType;}public void setFileUploadContentType(String fileUploadContentType) {this.fileUploadContentType = fileUploadContentType;}public String getFileUploadFileName() {return fileUploadFileName;}public void setFileUploadFileName(String fileUploadFileName) {this.fileUploadFileName = fileUploadFileName;}public String getMsg() {return msg;}public String upload(){String realPath = ServletActionContext.getServletContext().getRealPath("/file");File file = new File(realPath);if (!file.exists()) {file.mkdirs();}try {FileUtils.copyFile(fileUpload, new File(file, fileUploadFileName));} catch (IOException e) {e.printStackTrace();}System.out.println("上传路径:" + realPath + "\t文件类型:" + fileUploadContentType);msg = "上传成功";return "message";}public String execute(){return "success";}}
fileUpload.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'fileUpload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> </head> <body> <form action="${pageContext.request.contextPath }/control/file/operation_upload.action" enctype="multipart/form-data" method="post"> <input type="file" name="fileUpload" /> <input type="submit" value="上传"/> </form> </body></html>
struts-fileUpload.xml:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="file" extends="struts-default" namespace="/control/file"><action name="operation_*" class="blog.action.FileUpload" method="{1}" ><result name="message">/WEB-INF/page/message.jsp</result><result name="success">/WEB-INF/page/fileUpload.jsp</result></action></package></struts>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.multipart.maxSize" value="30000000"></constant><!-- 上传文件的大小限制 --><include file="struts-fileUpload.xml"></include></struts>
- struts2上传文件(三) struts2上传文件
- [Struts2]Struts2的文件上传
- Struts2的文件上传
- Struts2文件上传
- Struts2 上传文件实例
- struts2文件上传
- struts2文件上传
- Struts2的文件上传
- struts2-上传文件(1)
- struts2-上传文件(2)
- Struts2上传文件示例
- struts2文件上传
- Struts2实现文件上传
- struts2文件上传
- Struts2 文件上传
- ext + struts2 上传文件
- Struts2 文件上传
- Struts2的文件上传
- ActionBar with Tab
- qt4.7 字体字号设设置;中文乱码,
- C++中的接口设计
- 装饰模式——Decorator
- c++多继承机制
- struts2文件上传
- 体会break和continue的用法
- UITableView实现加载更多功能
- iOS 中CFUUIDRef、 CFUUIDCreate 、CFUUIDCreateString 生成唯一字符串
- CLisp 6:通过TCP/IP连接远程执行命令
- 什么是REST?以及RESTful的实现
- Android实现ListView圆角效果
- Ubuntu 10.04 右上角网络管理图标消失的解决办法
- myeclipse中项目不编译解决方法