package cn.axhu.education.action.file;
import cn.axhu.education.common.FileSystemConfig;
import cn.axhu.education.common.utils.FileUtils;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import org.json.JSONObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
/**
* Created by yi.wang1 on 2015/3/9.
*/
@ParentPackage("default")
@Namespace("/file")
public class ChapterAttachUploadAction extends SimpleActionSupport {
@Action("saveAttachFile")
public void saveAttachFile() {
PrintWriter out = null;
try {
//初始化变量
InputStream stream = null;
String ret_fileName = null;//返回给前端已修改的文件名称
Map<String, Object> result = new HashMap<String, Object>();
String ret_filePath;
File uploadedFile;
String uuid_fileName;
try {
//设置字符集
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
out = response.getWriter();
// 文件保存目录路径
String savePath = FileSystemConfig.getFileRootPath() + "chapterAttach/";
// 创建文件夹
File dirFile = new File(savePath);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
//获取文件流
MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) request;
if (multiWrapper.hasErrors()) {
for (Iterator errorIter = multiWrapper.getErrors().iterator(); errorIter.hasNext(); ) {
String error = (String) errorIter.next();
LOG.error(error);
}
}
File file = null;
Enumeration fileParameterNames = multiWrapper.getFileParameterNames();
while (fileParameterNames != null && fileParameterNames.hasMoreElements()) {
String inputName = (String) fileParameterNames.nextElement();
String[] contentType = multiWrapper.getContentTypes(inputName);
if (contentType != null) {
String[] fileName = multiWrapper.getFileNames(inputName);
if (fileName != null) {
ret_fileName = fileName[0];
File[] files = multiWrapper.getFiles(inputName);
file = files[0];
}
}
}
stream = new FileInputStream(file);
//初始化文件名,并定义上传文件
UUID uuid = UUID.randomUUID();
String suffix = ret_fileName.substring(ret_fileName.lastIndexOf(".") + 1);
uuid_fileName = uuid.toString() + "." + suffix;
ret_filePath = savePath + "/" + uuid_fileName + ".temp";
uploadedFile = new File(ret_filePath);
FileUtils.saveFile(uploadedFile, stream);
} catch (Exception e) {
//读写错误异常处理
result.put("message", e.getMessage());
out.print(new JSONObject(result).toString());
return;
} finally {
//资源关闭异常处理
try {
if (stream != null) {
stream.close();
}
} catch (Exception e) {
result.put("message", e.getMessage());
out.print(new JSONObject(result).toString());
return;
}
}
result.put("message", "success");
result.put("fileName", ret_fileName);
result.put("filePath", "chapterAttach/" + uuid_fileName + ".temp");
out.print(new JSONObject(result).toString());
} finally {
//将信息返回
if (out != null) {
out.flush();
out.close();
}
}
}
}