📄 fileoperator.java
字号:
package com.xuntian.material.util;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;
import com.xuntian.material.exception.ReportException;
public class FileOperator {
public static void downloadReport(HttpServletResponse response,
HSSFWorkbook wb) throws ReportException {
try {
response.setContentType("application/x-msdownload;charset=GBK");
response.setHeader("Content-Disposition", "filename=report.xls");
response.setCharacterEncoding("GBK");
wb.write(response.getOutputStream());
} catch (IOException e) {
throw new ReportException(e);
}
}
/**
* 文件下载
*
* @param filePath
* @param fileName
* @param response
* @throws IDCException
*/
public static void downloadFile(String filePath, String fileName,
HttpServletResponse response) throws FileNotFoundException,
IOException { // 下载指定文公的附件
BufferedOutputStream bos = null;
StringBuffer sb = new StringBuffer(50);
sb.append("attachment; filename=");
sb.append(fileName);
if (filePath != null && fileName != null) {
File file = new File(filePath);
if (!file.exists() || file.isDirectory()) {
throw new FileNotFoundException();
}
FileInputStream fis = new FileInputStream(file);
response.setContentType("application/x-msdownload;charset=GBK");
response.setHeader("Content-Disposition", new String(sb.toString()
.getBytes(), "ISO8859-1"));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[2048];
int l;
while ((l = fis.read(buffer)) != -1) {
bos.write(buffer, 0, l);
}
fis.close();
bos.close();
}
}
/**
* 文件上传
*
* @param form
* @param request
* @param filePath
* @return
* @throws IDCException
*/
public static Map uploadFile(ActionForm form, HttpServletRequest request,
String filePath) throws IOException {
Map<String, String> fileInfos = new HashMap<String, String>();
// 取得存放文件的物理路径
String attachmentPath = Constants.PATH_ATTATCHMENT + filePath
+ File.separator;
File attachmentDir = new File(attachmentPath);
if (!attachmentDir.exists())// 判断是否存在目录,不存在则创建目录
{
attachmentDir.mkdirs();
}
MultipartRequestHandler multipartRequestHandler = form
.getMultipartRequestHandler();
// 取得所有上传文件的对象集合
Hashtable elements = multipartRequestHandler.getFileElements();
// 循环遍历每一个文件
Collection values = elements.values();
int k = 0;
Iterator i = values.iterator();
while (i.hasNext()) {
FormFile file = (FormFile) i.next();// 取得上传的文件
// 判断文件是不是空,如果是空文件则忽略
if (file == null || file.getFileSize() == 0
|| file.getFileName().equals("")) {
continue;
}
// 构造文件的全路径包括文件名称
String fileName = file.getFileName();
String rename = DateTools.getNow("yyyyMMddHHmmssSSS") + (++k);
String fileFullPath = attachmentPath + rename;
// 调用保存文件的方法
saveFile(file, fileFullPath);
fileInfos.put(fileName, fileFullPath);
}
return fileInfos;
}
/**
* 保存文件
*
* @param file
* @param fileFullPath
* @throws IDCException
*/
public static void saveFile(FormFile file, String fileFullPath)
throws IOException {
InputStream stream = file.getInputStream();// 把文件读入
OutputStream bos = new FileOutputStream(fileFullPath);// 建立一个上传文件的输出流
// 开始读取文件
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);// 将文件写入服务器
}
bos.close();
stream.close();
}
/**
* 删除服务器上的文件
*
* @param files
*/
public static void deleteServerFiles(List<String> files) {
File file;
for (String filePath : files) {
if (filePath != null && !filePath.equals("")) {
file = new File(filePath);
file.delete();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -