/**
* 上传文件指定目录(公共方法)
*
* @author com.fhy.xyb
* @param in
* 上传文件字节流
* @param dir
* 上传文件物理路径
* @param fileName
* 上传文件名
* @return String 返回上传过后文件名
* @throws IOException
* 抛出IO异常
* @createTime 下午05:58:42
* @updateTime 下午05:58:42
*/
private String MatuploadFile(InputStream in, String dir, String fileName)
throws IOException {
fileName = fileName.substring(fileName.lastIndexOf("."));
String realName = UUID.randomUUID().toString() + fileName;
System.out.println("打印realName信息-------------" + realName);
File file = new File(dir, realName);
if (!file.exists()) {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
}
FileUtils.copyInputStreamToFile(in, file);
return realName;
}
/**
* 文件下载
*
* @author com.fhy.xyb
* @param request
* @param response
* @createTime 下午05:40:18
* @updateTime 下午05:40:18
*/
@RequestMapping("downloadMaterial.do")
public void downloadMaterial(HttpServletRequest request,
HttpServletResponse response) {
OutputStream os = null;
response.reset();
// 获取到用户下载文件路径
String realName = request.getParameter("realName");
try {
// 拆分文件下载路径
String name = realName.substring(realName.lastIndexOf("/"));
realName = "E:/" + realName;
response.setHeader("Content-Disposition", "attachment; filename="
+ name);
response.setContentType("application/octet-stream; charset=utf-8");
os = response.getOutputStream();
os.write(FileUtils.readFileToByteArray(new File(realName)));
os.flush();
} catch (Exception e) {
System.out.println("文件下载中断");
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
System.out.println("文件下载中断");
}
}
}
}
/**
* 根据路径删除指定的目录或文件(公共方法)
*
*@author com.fhy.xyb
*@param filePath 文件相对路径
*@return boolean
*@createTime 下午05:43:46
*@updateTime 下午05:43:46
*/
public boolean DeleteFolder(String filePath){
System.out.println("执行到删除素材物理文件业务==================");
File file = new File(filePath);
//判断目录或者文件是否存在
if(file.exists()){
//判断是否为文件
if(file.isFile()){
file.delete();
System.out.println("删除素材物理文件业务成功==================");
return true;
}else{
System.out.println("删除素材物理文件业务失败==================");
return false;
}
}else{
return false;
}
}