Java框架篇-struts之文件上传和下载
作者:网络转载 发布时间:[ 2015/4/3 11:02:55 ] 推荐标签:Java 框架 struts 文件上传
创建action类: 处理上传文件,
package com.oumyye.FileUpload;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
private File myFile;
private String myFileContentType;
private String myFileFileName;
private String destPath;
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
destPath = "e:/upload/";
try{
System.out.println("Src File name: " + myFile);
System.out.println("我的文件名"+myFileFileName);
System.out.println("我的文件类型"+ myFileContentType);
File destFile = new File(destPath, myFileFileName);
FileUtils.copyFile(myFile, destFile);
}catch(IOException e){
e.printStackTrace();
return ERROR;
}
return SUCCESS;
}
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
}
public String getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
public String getDestPath() {
return destPath;
}
public void setDestPath(String destPath) {
this.destPath = destPath;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
配置文件:
可以使用恒定的标签在应用程序 struts.xml文件,像我一样改变要上传的文件的大大小。让我们有我们的在struts.xml如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="false" />
<!-- 指定每次请求到达,重新加载资源文件 -->
<constant name="struts.i18n.reload" value="true" />
<!-- 指定每次配置文件更改后,自动重新加载 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 把主题配置为simple -->
<constant name="struts.ui.theme" value="simple" />
<!-- 指定允许上传的文件大字节数。默认值是2097152(2M) -->
<constant name="struts.multipart.maxSize" value="10701096" />
<!--文件上传-->
<package name="upload" namespace="/" extends="struts-default">
<action name="Upload" class="com.oumyye.FileUpload.FileUploadAction">
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
<!--文件下载-->
<action name="FileDownload" class="com.oumyye.action.FileDownload">
<result name="success" type="stream">
<param name="contentType">text/plain</param>
<param name="contentDisposition">attachment;fileName="${fileName}"</param>
<param name="inputName">downloadFile</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
以下是web.xml文件中的内容,与Struts2的基本配置一样
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Struts2_1000_FileUpload</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>*.action</url-pattern>
</filter-mapping>
</web-app>
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
Java性能测试有哪些不为众人所知的原则?Java设计模式??装饰者模式谈谈Java中遍历Map的几种方法Java Web入门必知你需要理解的Java反射机制知识总结编写更好的Java单元测试的7个技巧编程常用的几种时间戳转换(java .net 数据库)适合Java开发者学习的Python入门教程Java webdriver如何获取浏览器新窗口中的元素?Java重写与重载(区别与用途)Java变量的分类与初始化JavaScript有这几种测试分类Java有哪四个核心技术?给 Java开发者的10个大数据工具和框架Java中几个常用设计模式汇总java生态圈常用技术框架、开源中间件,系统架构及经典案例等

sales@spasvo.com