开始之前,先说点题外话
由于IDEA上传的文件静态资源,保存的路径和eclipse有些区别,之前用eclipse是保存在WEB-INF下,
当然我们也可以修改output directory,和eclipse一样放在WEB-INF目录下,操作如下,编辑tomcat
1
23
@RequestMapping(value = "download")
public void download(Catalog catalog,HttpServletRequest request, HttpServletResponse response){
//获取服务器绝对路径, 一般在项目下target路径
String rootPath = request.getSession().getServletContext().getRealPath("/");
//从数据库获取文件名(此名包括路径,且为URL编码需解码)
String info = catalog.getInfo();
String fileName = "";
try {
//将URL的文件名转码为UTF-8(调用java.net.URLDecoder的方法解码)
fileName = URLDecoder.decode(info, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//调用FileUtils.downFile下载(FileUtils已经写好的文件工具类)
FileUtils.downFile(new File(rootPath+fileName),request,response);
}
其实这样已经可以完成下载了,下面说下关于上传和下载中获取文件路径,还是以上面的下载为例分解说明
@RequestMapping(value = "download")
public void download(Catalog catalog,HttpServletRequest request, HttpServletResponse response){
//这里讲下获取上传文件的路径
//1.获取服务器的绝对路径,一般在项目下target路径 String rootPath = request.getSession().getServletContext().getRealPath("/");
//2.对于配置了basedir的可以直接调用方法 String rootPath = Global.getUserfilesBaseDir();
//3.对于上传指定了路径 String rootPath = "D:\\workspace\\file\\";
//获取数据库中文件名(此包括虚拟路径,且为URL编码)
String info = catalog.getInfo();
//java.io.File将数据库中"/"转义"\"
File file = new File(info);
String file1 = file.getPath();
String file2 = info.split("\\\\")[0];
//这里split("\\\\")做下说明,正则中以"\\"表示"\",java中也是以"\\"表示"\",所以即"\\\\"
String fileName1 = "";
String fileName2 = "";
//File中file.getParent()方法获取文件的上级目录
String path1 = file.getParent();
//用String.substring()方法截取文件的上级目录,另外也可以用split()方法截取,用split截取的是数组
String path2 =info.substring(info.indexOf("/"),info.lastIndexOf("/"));
//在示例单独截取只文件名和扩展名
String flName = info.substring(info.lastIndexOf("/")+1);
String flEtx = info.substring(info.lastIndexOf("."));
//下面一样需要解码
try {
//将URL编码的文件名解码为UTF-8格式(调用java.net.URLDecoder的方法解码)
fileName1 = URLDecoder.decode(file1, "UTF-8");
fileName2 = URLDecoder.decode(file2, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//调用FileUtils.downFile下载
FileUtils.downFile(new File(rootPath+fileName1),request,response);
}
以debug执行看看结果,如下
从结果看,从数据库中得到的文件和上级路径都是以"/"开头
(如URL: http://blog.youkuaiyun.com/tenlay_li/article/details/79311857 ),
经过java.io.File修正路径"\"(如 C:\Windows\System32\drivers )
jsp页面:
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>上传下载测试</title>
<meta name="decorator" content="default"/>
<script type="text/javascript">
$(document).ready(function() {
});
//上传
function upload(obj) {
var index = layer.open({
title : "上传资料",
type : 2,
content : "${ctx}/wl/catalog/upload?id="+obj,
area: ['50%', '50%']
})
}
//下载
function download(obj) {
var url = "${ctx}/wl/catalog/download?id=" + obj;
window.location.href=url;
}
</script>
</head>
<body>
<form id="searchForm" modelAttribute="wlCatalog" action="${ctx}/wl/catalog/" method="post" enctype="multipart/form-data" class="breadcrumb form-search">
<%--我这里是用onclick事件,也可以用a标签,不过a标签在鼠标放在上面就可以看到href的存放资源库地址,也可以说安全系数较低
<a href="${ctx}/wl/catalog/download" class="btn btn-primary">下载</a>--%>
</form>
<button type="button" class="btn" onclick="upload('${catalog.id}')" >上传</button>
<button type="button" class="btn" onclick="download('${catalog.id}')" >下载</button>
</body>
</html>
下面FileUtils写好了上传下载删除
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists;
/**
* 文件操作工具类
* 实现文件的创建、删除、复制、压缩、解压以及目录的创建、删除、复制、压缩解压等功能
*/
public class FileUtils extends org.apache.commons.io.FileUtils {
private static Logger logger = LoggerFactory.getLogger(FileUtils.class);
/**
* 复制单个文件,如果目标文件存在,则不覆盖
* @param srcFileName 待复制的文件名
* @param descFileName 目标文件名
* @return 如果复制成功,则返回true,否则返回false
*/
public static boolean copyFile(String srcFileName, String descFileName) {
return FileUtils.copyFileCover(srcFileName, descFileName, false);
}
/**
* 复制单个文件
* @param srcFileName 待复制的文件名
* @param descFileName 目标文件名
* @param coverlay 如果目标文件已存在,是否覆盖
* @return 如果复制成功,则返回true,否则返回false
*/
public static boolean copyFileCover(String srcFileName, String descFileName, boolean coverlay) {
File srcFile = new File(srcFileName);
// 判断源文件是否存在
if (!srcFile.exists()) {
logger.debug("复制文件失败,源文件 " + srcFileName + " 不存在!");
return false;
}
// 判断源文件是否是合法的文件
else if (!srcFile.isFile()) {
logger.debug("复制文件失败," + srcFileName + " 不是一个文件!");
return false;
}
File descFile = new File(descFileName);
// 判断目标文件是否存在
if (descFile.exists()) {
// 如果目标文件存在,并且允许覆盖
if (coverlay) {
logger.debug("目标文件已存在,准备删除!");
if (!FileUtils.delFile(descFileName)) {
logger.debug("删除目标文件 " + descFileName + " 失败!");
return false;
}
} else {
logger.debug("复制文件失败,目标文件 " + descFileName + " 已存在!");
return false;
}
} else {
if (!descFile.getParentFile().exists()) {
// 如果目标文件所在的目录不存在,则创建目录
logger.debug("目标文件所在的目录不存在,创建目录!");
// 创建目标文件所在的目录
if (!descFile.getParentFile().mkdirs()) {
logger.debug("创建目标文件所在的目录失败!");
return false;
}
}
}
// 准备复制文件
// 读取的位数
int readByte = 0;
InputStream ins = null;
OutputStream outs = null;
try {
// 打开源文件
ins = new FileInputStream(srcFile);
// 打开目标文件的输出流
outs = new FileOutputStream(descFile);
byte[] buf = new byte[1024];
// 一次读取1024个字节,当readByte为-1时表示文件已经读取完毕
while ((readByte = ins.read(buf)) != -1) {
// 将读取的字节流写入到输出流
outs.write(buf, 0, readByte);
}
logger.debug("复制单个文件 " + srcFileName + " 到" + descFileName
+ "成功!");
return true;
} catch (Exception e) {
logger.debug("复制文件失败:" + e.getMessage());
return false;
} finally {
// 关闭输入输出流,首先关闭输出流,然后再关闭输入流
if (outs != null) {
try {
outs.c