文件上传

package com.yutu.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yutu.biz.FileInfoBiz;
import com.yutu.biz.impl.FileInfoBizImpl;
import com.yutu.entity.FileInfo;
import com.yutu.util.MD5;

public class FileActio extends ActionSupport {
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
private String savePath;
private FileInfoBiz fb = new FileInfoBizImpl();

// private List<String> successImages;

// public List<String> getSuccessImages() {
// return successImages;
// }

// public void setSuccessImages(List<String> successImages) {
// this.successImages = successImages;
// }

public File[] getUpload() {
return upload;
}

public void setUpload(File[] upload) {
this.upload = upload;
}

public String[] getUploadContentType() {
return uploadContentType;
}

public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}

public String[] getUploadFileName() {
return uploadFileName;
}

public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}

public String getSavePath() {
return savePath;
}

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

public String uploadFile() {
// 流对象
FileInputStream fis = null;
FileOutputStream fps = null;
// 获取文件要保存位置的真实磁盘路径
String realPath = ServletActionContext.getServletContext().getRealPath(
savePath);

// 集合初始化 保存上传成功的文件的相对目录的路径的集合
// successImages = new ArrayList<String>();

// 遍历
for (int i = 0; i < upload.length; i++) {
try {
File dir = new File(realPath);// 创建文件对象,磁盘不一定存在在此路径的文件
if (!dir.exists()) {// 如果文件对象不存在(对应的文件对象在磁盘中是否存在)
dir.mkdirs();// 创建路径中所有的不存在文件对象
}
String index = uploadFileName[i].substring(uploadFileName[i]
.indexOf("."));
System.out.println("sssssssssss" + savePath);
// 创建文件对象
FileInfo fileInfo = new FileInfo();
fileInfo.setSize(upload[i].length());
fileInfo.setFileName(uploadFileName[i]);

// 获取要上传文件
fis = new FileInputStream(upload[i]);// 指向上传的文件 输入
// 调用方法获取文件
String fileName = MD5.MD5(fis, fileInfo.getSize());
fileInfo.setNo(fileName);// 以MD5作为主键
fileInfo.setContentType(uploadContentType[i]);// 获取对象文件类型

File saveFile = new File(dir, fileName + index);
if (saveFile.exists()) {// 判断文件在磁盘中是否存在
continue;// 如果已经存在,则不需要copy
}
if (saveFile.createNewFile()) {// 重新创建文件
fps = new FileOutputStream(saveFile);// 指向保存的文件 输出
IOUtils.copy(fis, fps);// 将输入流文件复制到输出流

// 保存的路径为 upladDir/文件名称
// successImages.add(savePath.substring(1) + "/"
// + uploadFileName[i]);
fileInfo.setRealPath(saveFile.getPath());// 获取真实路径 用流获取 下载使用

fileInfo.setPath(savePath.substring(1) + "/" + fileName
+ index);// 获取相对路径 用在页面显示
System.out.println(fileInfo.getPath());

// 保存FileInfo对象

fb.save(fileInfo);
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 关闭流,释放资源 先释放输出流
try {
if (fps != null) {
fps.close();
}
} catch (IOException e) {
}
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
}

}
}
return SUCCESS;
}

/**
* 查询文件集合
*
* @return
*/
public String fileList() {
List<FileInfo> infos = fb.queryFiles();
// 解耦方式 获取request 对象
Map<String, Object> reques = (Map<String, Object>) ActionContext
.getContext().get("request");
reques.put("list", infos);
return SUCCESS;
}

}

 

action  的配置

<action name="uploa" class="com.yutu.action.FileActio" method="uploadFile">
<param name="savePath">/uploadDir</param>
<result name="success" type="redirectAction">list</result>
<result name="error">error.jsp</result>
</action>

posted @ 2017-02-21 11:15  codedom  阅读(120)  评论(0编辑  收藏  举报