package com.brilliant.common.toolkit.io;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.brilliant.common.toolkit.system.Application;
import com.oreilly.servlet.MultipartRequest;
@SuppressWarnings("serial")
public class CosUtil implements Serializable {
/**
* File upload method.
*
* @param request(HttpServletRequest)
* @param filePath(String) - 指定所上传文件,在服务器端的存储目录
* @param filesMaxSize(Integer) - 限制上传文件的合计大小。默认为5MB
* @return (Map)
* @throws IOException
*/
@SuppressWarnings( { "unchecked", "rawtypes" })
public static Map upload(HttpServletRequest request, String filePath, Integer filesMaxSize) throws IOException {
// 存储路径
if(filePath == null){
filePath = Application.getInstance().getWebRootPath() + "upload";
}
File uploadPath = new File(filePath);
// 检查文件夹是否存在,不存在,创建
if (!uploadPath.exists()) {
uploadPath.mkdirs();
}
// 上传文件的合计最大容量(默认:5M)
if(filesMaxSize == null){
filesMaxSize = 5;
}
int _filesMaxSize = filesMaxSize * 1024 * 1024;
// 本次上传文件的信息集合
Map info = new HashMap();
List<Map> files = new ArrayList<Map>();
// 重命名策略
FileRenameProxy rfrp = new FileRenameProxy();
// 上传文件
MultipartRequest multi = new MultipartRequest(request, filePath,
_filesMaxSize, "UTF-8", rfrp);
// 获得上传文件信息
Enumeration filesname = multi.getFileNames();
while (filesname.hasMoreElements()) {
Map file = new HashMap();
String name = (String) filesname.nextElement();
file.put("originalFileName", multi.getOriginalFileName(name));
file.put("fileName", multi.getFilesystemName(name));
file.put("contentType", multi.getContentType(name));
files.add(file);
}
info.put("files", files);
// 获得提交的表单中其它输入域的值
Enumeration formElements = multi.getParameterNames();
while(formElements.hasMoreElements()){
String param = (String)formElements.nextElement();
String[] values = multi.getParameterValues(param);
info.put(param, values);
}
return info;
}
/**
* File download method.
*
* @param response(HttpServletRequest)
* @param filePath(String) - 指定所下载文件,在服务器端的存储目录
* @param isOnLine(boolean) - 是否在线打开文件
* @throws Exception
*/
public static void download(HttpServletResponse response, String filePath, boolean isOnLine) throws Exception {
// try {
// System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$ download $$$$$$$$$$$$$$$$$$$$$$$$\n" + filePath + "\n" +
// URLDecoder.decode(filePath, "utf-8") + "\n" +
// URLDecoder.decode(URLDecoder.decode(filePath, "utf-8"), "utf-8"));
// } catch (UnsupportedEncodingException e1) {
// e1.printStackTrace();
// }
// 要求文件名在浏览器端用 Javascript 编码(encode)2次
File f = new File(URLDecoder.decode(URLDecoder.decode(filePath, "UTF-8"), "UTF-8"));
if (f.exists()) {
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
if (isOnLine) {
// 在线打开方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
} else {
// 纯下载方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0){
out.write(buf, 0, len);
}
br.close();
out.close();
} else {
response.sendError(404, "File not found!");
}
}
}