structs利用 FileUtils 类文件上传示例
来源:互联网 发布:韩国文化产业知乎 编辑:程序博客网 时间:2024/05/16 12:48
运用 FileUtils 类
利用 apache 的 IO 流的 FileUtils 类实现操作
在structs的jar包中,有 commons-io-2.2.jar
参考:http://snkcxy.iteye.com/blog/1845862 有更多文件读写,删除操作
文件copy操作
//结果是cxyapi和cxyapi1在同一目录 FileUtils.copyDirectory(new File("D:/cxyapi"), new File("D:/cxyapi1")); //结果是将cxyapi拷贝到cxyapi2下 FileUtils.copyDirectoryToDirectory(new File("D:/cxyapi"), new File("D:/cxyapi2")); //拷贝文件 FileUtils.copyFile(new File("d:/cxyapi.xml"), new File("d:/cxyapi.xml.bak")); //拷贝文件到目录中 FileUtils.copyFileToDirectory(new File("d:/cxyapi.xml"), new File("d:/cxyapi")); //拷贝url到文件 FileUtils.copyURLToFile(new URL("http://www.cxyapi.com/rss/cxyapi.xml"), new File("d:/cxyapi.xml"));
把D:/TestCopy/test2.jpg 文件拷贝一份到目录下的 test3.jpg
try { FileUtils.copyFile(new File("D:/TestCopy/test2.jpg"), new File("D:/TestCopy/test3.jpg")); } catch (IOException e) { e.printStackTrace(); }
structs文件单文件上传示例
structs.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="default" namespace="/" extends="struts-default"> <action name="uploadaction" class="action.UploadAction" method="upload"> <result>/index.jsp</result> </action> </package></struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>structs2upload</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
UploadAction.java
//package action;import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")public class UploadAction extends ActionSupport{ private File uploadImage;//得到上传的文件 private String uploadContentType;//得到文件的类型 private String uploadImageFileName;//得到文件的名称 @Override public String execute() throws Exception { return super.execute(); } public String upload(){ String realpath = ServletActionContext.getServletContext().getRealPath("/images"); System.out.println("文件存放路径"+realpath);//自己也可以定义 File file = new File(realpath); if(!file.exists()) file.mkdir();//创建目录 try { FileUtils.copyFile(uploadImage, new File(file,uploadImageFileName)); } catch (IOException e) { e.printStackTrace(); } return SUCCESS; } public File getUploadImage() { return uploadImage; } public void setUploadImage(File uploadImage) { this.uploadImage = uploadImage; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public String getUploadImageFileName() { return uploadImageFileName; } public void setUploadImageFileName(String uploadImageFileName) { this.uploadImageFileName = uploadImageFileName; }}
upload.jsp
<%@ 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="uploadaction" method="post" enctype="multipart/form-data"> <input type="file" name="uploadImage"/> <br> <input type="submit" value="upload"/> </form></body></html>
index.jsp
<%@ 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>上传成功</body></html>
文件上传到的目录(workspace目录)
D:\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\structs2upload\images
structs文件多文件上传示例
同时上传两个文件
upload.jsp
..<body> <form action="uploadaction" method="post" enctype="multipart/form-data"> <input type="file" name="uploadImage"/><br/> <input type="file" name="uploadImage"/><br/> <br> <input type="submit" value="upload"/> </form></body>..
UploadAction.java
//package action;import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")public class UploadAction extends ActionSupport{ private File[] uploadImage;//得到上传的文件 private String[] uploadContentType;//得到文件的类型 private String[] uploadImageFileName;//得到文件的名称 @Override public String execute() throws Exception { return super.execute(); } public String upload(){ String realpath = ServletActionContext.getServletContext().getRealPath("/images"); System.out.println(realpath); File file = new File(realpath); if(!file.exists()) file.mkdir();//创建目录 try { for(int i=0;i<uploadImage.length;i++){ FileUtils.copyFile(uploadImage[i], new File(file,uploadImageFileName[i])); } //FileUtils.copyFile(uploadImage, new File(file,uploadImageFileName)); } catch (IOException e) { e.printStackTrace(); } return SUCCESS; } public File[] getUploadImage() { return uploadImage; } public void setUploadImage(File[] uploadImage) { this.uploadImage = uploadImage; } public String[] getUploadContentType() { return uploadContentType; } public void setUploadContentType(String[] uploadContentType) { this.uploadContentType = uploadContentType; } public String[] getUploadImageFileName() { return uploadImageFileName; } public void setUploadImageFileName(String[] uploadImageFileName) { this.uploadImageFileName = uploadImageFileName; } }
web传递文件必须要求文件大小比较小,(2,3M),文件太大,http上传速度慢
如果是大量文件上传通过socket通讯传递
structs中有常量可以来限制文件上传的大小
<!-- 上传文件的10M大小限制--> <constant name="struts.multipart.maxSize" value="11943040"/>
0 0
- structs利用 FileUtils 类文件上传示例
- structs简单文件上传
- structs做文件上传
- 文件工具类FileUtils
- FileUtils文件工具类
- 使用structs上传文件到服务器
- FileUtils 文件管理公共类
- 文件操作工具类FileUtils
- 文件操作工具类FileUtils
- 文件操作工具类FileUtils
- lua-FileUtils文件工具类
- 文件操作工具类FileUtils
- FileUtils 文件工具类org.apache.commons.io.FileUtils
- Android WebView上传的FileUtils工具类
- android 处理txt文件类FileUtils,利用java反射机制访问非sdk类和函数
- android 处理txt文件类FileUtils,利用java反射机制访问非sdk类和函数
- 利用commons-io.jar包中FileUtils和IOUtils工具类操作流及文件
- Android 文件操作帮助类FileUtils文件
- html<font>标签的color属性以及<h1> 到 <h6> 标签
- 浅谈一点对VR游戏的看法。
- 使用Linux自带的i2c-dev操作EEPROM
- Oracle使用——PLSQL的中文乱码显示全是问号
- Android OTA升级原理和流程分析(四)---Android系统Recovery模式的工作原理
- structs利用 FileUtils 类文件上传示例
- JQuery 加载并解析 XML实现省市联动
- linux下创建软连接和硬链接
- <转>hadoop学习之hadoop集群功能简单测试验证
- python字符转码问题-持续更新
- 利用JDK开发WebService简单实现
- iOS NSScanner类的基本用法详解
- LeetCode84. Largest Rectangle in Histogram
- Spark 数据ETL