/**上传文件
* @author丁思
* @date 2015年10月21
* @param fileName 文件名
* @param businessNumber 文件唯一编号
* @param file 文件对象
* @param log 日志对象
* @param msg 日志错误信息
*/
public String upload(String fileName,String businessNumber,File file,Log log,String msg){
String newFileName = "",//新文件名
secondName = "";//文件后缀
if (Util.notEmpty(file) && Util.notEmpty(fileName)) {
try {
file.setWritable(true, false);//[设置对除java程序的目录有写的权限]
fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
String[] s = fileName.split("\\");
fileName = s[s.length - 1];// 文件原名
String[] name = fileName.split("\.");
secondName = name[name.length - 1];// 文件后缀
newFileName = businessNumber+"."+secondName;
String path = PropertieConstantsUtil.getProperty("ID_UPLOAD_PATH") + File.separator;
ServletResponse.setContentType("text/html;charset=utf-8");
ServletResponse.setCharacterEncoding("utf-8");
ServletResponse.setHeader("Charset", "utf-8");
ServletResponse.setHeader("Cache-Control", "max-age=0");
ServletResponse.getWriter().flush();
ServletResponse.getWriter().close();
FileInputStream inputStream = new FileInputStream(file);
FileOutputStream outputStream = new FileOutputStream(path+ File.separator +newFileName);
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
doLog("上传"+msg+"异常!", log, e);
}
}
return "json";
}
/**下载文件
*@author 丁思
* @date 2015年10月14日阳
* @param fileName 文件名
* @param log 日志对象
* @param msg 日志提示信息
*/
public void downLoad(String fileName,Log log,String msg){
try {
// path是指欲下载的文件的路径。
String path = PropertieConstantsUtil.getProperty("ID_UPLOAD_PATH") + File.separator+ fileName;
// 初始化HttpServletResponse对象
HttpServletResponse response = ServletActionContext.getResponse();
// 创建一个输出流对象
OutputStream out = new BufferedOutputStream(response.getOutputStream());
String sysEncdoe = ProcessExcel.getSystemFileCharset();
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
fileName = new String(fileName.getBytes(sysEncdoe), "iso8859-1");// headerString为中文时转码
response.setHeader("Content-disposition", "attachment; filename="+ fileName);// filename是下载的xls的名,建议最好用英文
response.setContentType("application/octet-stream;charset=UTF-8");// 设置类型
response.setHeader("Pragma", "public");// 设置头
response.setHeader("Cache-Control", "max-age=0");// 设置头
response.setDateHeader("Expires", 0);// 设置日期头
out.write(buffer);
out.flush();
out.close();
response.flushBuffer();
} catch (IOException e) {
doLog("下载"+msg+"异常!", log, e);
}
}
/**删除文件
* @author 丁思
* @date 2015年10月14日阳
* @param fileName 文件名
* @param log 日志对象
* @param msg 日志提示信息
*/
public void remove(String fileName,Log log,String msg){
try{
String path = PropertieConstantsUtil.getProperty("ID_UPLOAD_PATH") + File.separator+ fileName;
File file = new File(path);
file.delete();
}catch(Exception e){
doLog("删除"+msg+"异常!", log, e);
}
}
下载模板,比如系统中录入数据需要用eccel的时候,我们需要下载自己的模板
public void expRemitoutAdd(){
HttpServletResponse response = ServletActionContext.getResponse();
try{
InputStream input = ServletActionContext.getServletContext().getResourceAsStream("/WEB-INF/abc.xlsx");
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("汇出汇款明细模板.xls","UTF-8"));
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
while ((len = input.read(b)) > 0) {
response.getOutputStream().write(b, 0, len);
}
input.close();
}catch (IOException e) {
e.printStackTrace();
}
}