使用struts2进行文件上传
2017-11-15 17:09
363 查看
1.写一个简单的jsp页面
2.写一个action,除了username 参数,其它写法固定
配置struts.xml
因为是struts2 ,别忘记在xml里配置前端控制器哦,经过测试,代码有效
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <form action="${pageContext.request.contextPath }/demo01_fun01" enctype="multipart/form-data" method="post"> <input type="file" name=upload_fileName/> <input type="text" name="username"/> <input type="submit"/> </form> </body> </html>
2.写一个action,除了username 参数,其它写法固定
public class action extends ActionSupport { private File upload_fileName; //文件name 标签属性名 //固定写法 private String upload_fileContentType;// 上传文件类型 private String upload_fileFileName;// 上传的文件名称 private String username; public void setUsername(String username) { this.username = username; } //提供set 方法 public void setUpload_fileName(File upload_fileName) { this.upload_fileName = upload_fileName; } public void setUpload_fileContentType(String upload_fileContentType) { this.upload_fileContentType = upload_fileContentType; } public void setUpload_fileFileName(String upload_fileFileName) { this.upload_fileFileName = upload_fileFileName; } public String fun01(){ System.out.println(username); try { File file = new File("D:\\"+upload_fileFileName); //使用apache提供的io工具类保存 FileUtils.copyFile(upload_fileName,file); } catch (IOException e) { e.printStackTrace(); } finally { return null; } } }
配置struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="demo" extends="struts-default" namespace="/"> <action name="demo01_*" class="cn.itcast.upload.action" method="{1}"> </action> </package> </struts>
因为是struts2 ,别忘记在xml里配置前端控制器哦,经过测试,代码有效
相关文章推荐
- 使用Struts2的文件上传机制,对上传文件大小进行限制
- 如何使用HttpUnit进行上传文件测试
- 如何在struts2中结合HttpClient进行文件上传
- AXIS:使用web service进行文件的上传
- 使用apache.commons.fileupload 进行文件上传
- FCKeditor是使用非常广泛的HTML编辑器,本文从 ASP.NET 的使用场景对 FCKeditor 与 FCKeditor.NET 的配置、功能扩展(如自定义文件上传子目录、自定义文件名、上传图片的后期处理等)、以及安全性进行初步的阐述。
- DiscuzNT使用Silverlight进行多文件上传
- DiscuzNT使用Silverlight进行多文件上传(下) 推荐
- 使用apache的fileupload进行文件上传
- 使用API进行FTP文件上传和下载
- DiscuzNT使用Silverlight进行多文件上传
- struts2学习笔记(10)——使用fileupload进行文件上传
- DiscuzNT使用Silverlight进行多文件上传
- 使用edtftpj进行ftp文件上传和下载
- 使用spring MVC框架进行文件上传
- 使用struts2中的文件上传
- 使用FLEX进行多文件上传和自定义上传信息
- liferay中使用liferay-ui进行文件上传
- 使用wininet进行FTP文件上传和下载
- 如何使用HttpUnit进行上传文件测试