返回首页
当前位置: 主页 > 编程语言 > JAVA教程 >

Struts2文件上传下载 实现代码

时间:2014-08-20 22:33来源:电脑教程学习网 www.etwiki.cn 编辑:admin

先看下文件结构

Struts2文件上传下载 实现代码
 

下面是 源码及其相关注释

package com;

import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownLoadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String savePath = "file//";//文件上传的路径
private String fileFileName;
private File file;
private String fileContentType;

public String getFileName() {
return fileFileName;
}

public void setFileName(String fileName) {
this.fileFileName = fileName;
}

public File getFile() {
return file;
}

public void setFile(File file) {
this.file = file;
}

public String getFileContentType() {
return fileContentType;
}

public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}

public String getFileFileName() {
return fileFileName;
}

public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}

public String getSavePath() {
return savePath;
}

public void setSavePath(String savePath) {
this.savePath = savePath;
}

@Override
public String execute() throws Exception {
fileFileName = ServletActionContext.getRequest().getParameter(
"fileName");
return SUCCESS;
}

// 从下载文件原始存放路径读取得到文件输出流
public InputStream getDownloadFile() throws UnsupportedEncodingException {
fileFileName = new String(fileFileName.getBytes("ISO8859-1"), "UTF-8");
System.out.println("fileFileName:Down:"+fileFileName);
return ServletActionContext.getServletContext().getResourceAsStream(
savePath + fileFileName);
}

// 如果下载文件名为中文,进行字符编码转换
public String getDownloadChineseFileName() {
String downloadChineseFileName = fileFileName;

try {
downloadChineseFileName = new String(downloadChineseFileName
.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

return downloadChineseFileName;
}


}
 

package com;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

private static final long serialVersionUID = 1L;
private File[] uploadFile;//上传的文件名
private String[] contentType;

private String[] caption;
private String[] uploadFilename;

public String[] getCaption() {
return caption;
}

public void setCaption(String[] caption) {
this.caption = caption;
}

public String[] getContentType() {
return contentType;
}

public void setUploadFileContentType(String[] contentType) {
this.contentType = contentType;
}

public File[] getUploadFile() {
return uploadFile;
}

public void setUploadFile(File[] uploadFile) {
this.uploadFile = uploadFile;
}

public String[] getUploadFilename() {
return uploadFilename;
}

public void setUploadFileFileName(String[] uploadFilename) {
this.uploadFilename = uploadFilename;
}

/**
* 拷贝文件到相应的目录下
* @param src
* @param dst
*/
private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src));
out = new BufferedOutputStream(new FileOutputStream(dst));
byte[] buffer = new byte[16 * 1024];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public String execute() {
for (int i = 0; i < this.getUploadFile().length; i++) {
File imageFile = null;
imageFile = new File(ServletActionContext.getServletContext()

------分隔线----------------------------
标签(Tag):java java技巧 java实例教程 java源代码 java基础教程
------分隔线----------------------------
推荐内容
  • IDEA初始化配置 图解教程

    使用JetBrains IDEA已经两个月了,今天给我自动升级了,但是一直报一个错误(大致就是...

  • java中数组转为列表的几种方法

    java中数组转为集合List,是很基础常见的,日常开发中也经常会用到,今天整理了下,有...

  • java常用设计模式-策略设计模式

    java中常用的设计模式如单例模式、原型模式,这在之前的2篇文章中已经说明,还有代理...

  • Java开发规范

    编码规范就如同协议,有了Http、TCP等各种协议,计算机之间才能有效地通信,同样的,...

  • java泛型那些事

    泛型的类型安全性 有许多原因促成了泛型的出现,而最引人注意的一个原因,就是为了创...

  • HdfsSink原理解析

    我们先了解几个概念: batchSize sink从channel中取batchSize数量的event才会flush,sy...

猜你感兴趣