【SSM】AppFileUtils
11
package com.kikyo.sys.utils; import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Properties; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; public class AppFileUtils { /**
* 得到文件上传的路径
*/
public static String PATH = "E:/upload/"; static {
InputStream stream = AppFileUtils.class.getClassLoader().getResourceAsStream("config/file.properties");
Properties properties = new Properties();
try {
properties.load(stream);
PATH = properties.getProperty("path");
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 文件下载
*
* @param response
* @param path
* @param oldName
* @return
*/
public static ResponseEntity<Object> downloadFile(HttpServletResponse response, String path, String oldName) {
//4,使用绝对路径+相对路径去找到文件对象
File file = new File(AppFileUtils.PATH, path);
//5,判断文件是否存在
if (file.exists()) {
try {
try {
//如果名字有中文 要处理编码
oldName = URLEncoder.encode(oldName, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
//把file转成一个bytes
byte[] bytes = FileUtils.readFileToByteArray(file);
HttpHeaders header = new HttpHeaders();
//封装响应内容类型(APPLICATION_OCTET_STREAM 响应的内容不限定)
header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//设置下载的文件的名称
header.setContentDispositionFormData("attachment", oldName);
//创建ResponseEntity对象
ResponseEntity<Object> entity = new ResponseEntity<Object>(bytes, header, HttpStatus.CREATED);
return entity;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} else {
PrintWriter out;
try {
out = response.getWriter();
out.write("文件不存在");
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
} /**
* 根据相对路径删除硬盘上文件
*
* @param path2
*/
public static void deleteFileUsePath(String path) {
String realPath = PATH + path;
//根据文件
File file = new File(realPath);
if (file.exists()) {
file.delete();
}
}
}
【SSM】AppFileUtils的更多相关文章
- 模仿天猫实战【SSM】——总结
第一篇文章链接:模仿天猫实战[SSM版]--项目起步 第二篇文章链接:模仿天猫实战[SSM版]--后台开发 总结:项目从4-27号开始写,到今天5-7号才算真正的完工,有许多粗糙的地方,但总算完成了, ...
- 【ssm】spring功能讲解
概览 Spring5框架包含许多特性,负责管理项目中的所有对象,并被很好地组织在下图所示的模块中 核心容器:由spring-beans.spring-core.spring-context.sprin ...
- 【SSM】Eclipse使用Maven创建Web项目+整合SSM框架
自己接触ssm框架有一段时间了,从最早的接触新版ITOO项目的(SSM/H+Dobbu zk),再到自己近期来学习到的<淘淘商城>一个ssm框架的电商项目.用过,但是还真的没有自己搭建过, ...
- 【SSM】---增删改查
20:43:06 package com.chinasofti.dao; import java.util.List; import com.chinasofti.entity.User; publi ...
- 【SSM】拦截器的原理、实现
一.背景: 走过了双11,我们又迎来了黑色星期五,刚过了黑五,双12又将到来.不管剁手的没有剁手的,估计这次都要剁手了!虽然作为程序猿的我,没有钱但是我们长眼睛了,我们关注到的是我们天猫.淘宝.支付宝 ...
- 【ssm】拦截器的原理及实现
一.背景: 走过了双11,我们又迎来了黑色星期五,刚过了黑五,双12又将到来.不管剁手的没有剁手的,估计这次都要剁手了!虽然作为程序猿的我,没有钱但是我们长眼睛了,我们关注到的是我们天猫.淘宝.支付宝 ...
- 【SSM】自定义属性配置的使用
首先,建立xxx.properties 文件在resource文件夹中,此处我们自定义的配置文件是oj-config.properties 然后,在applicationContext.xml中注册这 ...
- 【SSM】---Spring+SpringMVC+Mybatis框架整合
参考 百度经验 https://jingyan.baidu.com/article/2a1383288a85a9074a134f1b.html CSDN http://blog.csdn.net/ge ...
- 【SSM 】导出excel含图片
ExprotRentUtils package com.kikyo.stat.utils; import java.awt.image.BufferedImage; import java.io.By ...
随机推荐
- Java8 Time API与老Date之间的转换
前面我已经总结了Java8 Time API常用的一些方法.封装的工具类,可是最近需要对一个比较老的项目进行重构,大致看了一下使用的Jdk还是7而且里面的时间工具类还是使用的Date和Calendar ...
- MongoDB高可用集群配置方案
原文链接:https://www.jianshu.com/p/e7e70ca7c7e5 高可用性即HA(High Availability)指的是通过尽量缩短因日常维护操作(计划)和突发的系统崩溃(非 ...
- rabbitmq 命令行与控制台
命令行和管控台 rabbitmqctl stop_app 关闭应用 rabbitmqctl start_app 打开应用 rabbitmqctl status 节点状态 rabbitmqctl add ...
- Flask 教程 第十八章:Heroku上的部署
本文翻译自The Flask Mega-Tutorial Part XVIII: Deployment on Heroku 这是Flask Mega-Tutorial系列的第十八部分,我将在其中部署M ...
- Linux - Shell - 字符串拼接
概述 shell 的字符串拼接 1. 字符串声明 概述 字符串的基本操作 脚本 1 # 声明字符串 str01="str01" echo ${str01} # 单引号也可以 # 不 ...
- label 阻止冒泡 防止点击label 触发2次事件
// 必须要把 jnput的外面的label加上事件阻止冒泡,否则点击label的时候,会冒泡到input上 再次触发input的点击事件 $('.xt_order_cleft_modb_rl_dx' ...
- laravel搭建博客实战的教程实例
这篇文章主要给大家介绍了关于利用laravel搭建一个迷你博客的相关资料,文中将一步步的实现步骤通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着来一起学习学习吧. 本文主 ...
- OGG在windows环境下字符集的配置
windows环境下不配置字符集(默认使用windows自己的字符集),从linux等系统同步过来的表中如果含有中文字符列将显示为乱码,被ogg误认为虚拟列,从而导致进程abend. 设置ogg进程在 ...
- Floyd-Warshall
Description 第一行四个数为n,m,n表示顶点个数,m表示边的条数. 接下来m行,每一行有三个数t1.t2 和t3,表示顶点t1到顶点t2的路程是t3.请注意这些t1->t2是单向的. ...
- 3、高级方法(Advanced Recipes)
学习目录:树莓派学习之路-GPIO Zero 官网地址:https://gpiozero.readthedocs.io/en/stable/recipes_advanced.html 环境:Ubunt ...