上传工具类
import java.io.File;
import java.io.FileInputStream;import java.io.FileOutputStream;
import java.text.DecimalFormat;
public class FileUtil {
public static String saveFile(File tmp, String savePath, String saveName) throws Exception{
String fileSavePath = "";
FileOutputStream os = null;
FileInputStream is = null;
try {
File saveDir = new File(savePath);
if (!saveDir.exists()) {
saveDir.mkdirs();
}
File of = new File(saveDir, saveName);
os = new FileOutputStream(of);
is = new FileInputStream(tmp);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
is.close();
os.close();
fileSavePath = of.getAbsolutePath();
} catch (Exception ex) {
ex.printStackTrace();
} finally{
if (is != null)
is.close();
if (os != null)
os.close();
}
return fileSavePath;
}
public static String getFileExtends(String fileName) {
String defExt = "pdf";
if ((fileName != null) && (fileName.length() > 0)) {
int i = fileName.lastIndexOf('.');
if ((i > -1) && (i < (fileName.length() - 1))) {
return fileName.substring(i + 1);
}
}
return defExt;
}
public static long getFileSize(File f)// 取得文件夹大小
{
long size = 0;
size = f.length();
return size;
}
public static String FormetFileSize(long fileS) {// 转换文件大小
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
if (fileS < 1024) {
fileSizeString = df.format((double) fileS) + "B";
} else if (fileS < 1048576) {
fileSizeString = df.format((double) fileS / 1024) + "K";
} else if (fileS < 1073741824) {
fileSizeString = df.format((double) fileS / 1048576) + "M";
} else {
fileSizeString = df.format((double) fileS / 1073741824) + "G";
}
return fileSizeString;
}
}
下载Action
public void download() throws Exception{
// 写流文件到前端浏览器
TBFile tbFile = fileOperService.getTbFile(fileId);
if (tbFile != null) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
String path = tbFile.getFilePath();
String fileName = tbFile.getFileRealName();
// 检查文件是否存在
File obj = new File(path);
if (!obj.exists()) {
ActionResult actionResult = new ActionResult(ConstanData.FAILURECODE,"请求的文件不存在请与管理员联系!");
response.getWriter().print(ContextUtil.resultToJson(actionResult));
return;
}
ServletOutputStream out = response.getOutputStream();
response.setHeader("Content-disposition", "attachment;filename=" + getEncodeFileName(request, fileName));
bis = new BufferedInputStream(new FileInputStream(path));
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
}
public void delete() throws Exception{
}
/**
* 页面文件名编码的方法(防止乱码出现)
*
* @param request
* @param fileName
* @return
* @throws UnsupportedEncodingException
*/
private static String getEncodeFileName(HttpServletRequest request, String fileName) throws UnsupportedEncodingException {
String encodeFileName = fileName;
// 浏览器取得判断
String agent = request.getHeader("USER-AGENT");
// IE
if (null != agent && -1 != agent.indexOf("MSIE")) {
encodeFileName = URLEncoder.encode(fileName, "UTF-8");
// 处理文件名中出现的空格
// 其中%20是空格在UTF-8下的编码
encodeFileName = encodeFileName.replace("+", "%20");
}
// FF
else if (null != agent && -1 != agent.indexOf("Mozilla")) {
encodeFileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(fileName.getBytes("UTF-8")))) + "?=";
}
return encodeFileName;
}
private static String encodingFileName(String fileName) {
String returnFileName = "";
try {
returnFileName = URLEncoder.encode(fileName, "UTF-8");
if (returnFileName.length() > 150) {
returnFileName = new String(fileName.getBytes("GB2312"), "ISO8859-1");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return returnFileName;
}
}
下载请求js
var url = "${pageContext.request.contextPath}/admin/fileDownload.dhtml?fileId="+fileId
window.location = url;