Spring MVC上传、下载 文件
1,上传文件
public static String upload(MultipartFile file, SysUserBean sysUserBean, HttpServletRequest request){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = new Date();
String path = request.getSession().getServletContext().getRealPath("upload\\"+sysUserBean.getId()+"\\"+sdf.format(date));
String fileLastName = "";
String fileName = "";
if(file != null){
int dot = file.getOriginalFilename().lastIndexOf('.');
if(dot>0){
fileLastName = file.getOriginalFilename().substring(dot);
fileName = UUID.randomUUID()+fileLastName;
File targetFile = new File(path, fileName);
if(!targetFile.exists()){
targetFile.mkdirs();
}
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
return "";
}
return "upload/"+sysUserBean.getId()+"/"+sdf.format(date)+"/"+fileName;
}else{
return "";
}
}
return "";
}
2,下载文件
public static void download(String fileName, HttpServletRequest request,
HttpServletResponse response)throws Exception {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("UTF-8");
java.io.BufferedInputStream bis = null;
java.io.BufferedOutputStream bos = null; String path = request.getSession().getServletContext().getRealPath("\\");
String downLoadPath = path + fileName;
System.out.println(downLoadPath);
try {
long fileLength = new File(downLoadPath).length();
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename="
+ new String(fileName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} }
3,base64 转成图片
// 对字节数组字符串进行Base64解码并生成图片
public static String GenerateImage(String imgStr,HttpServletRequest request,String userid) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = new Date();
if (imgStr == null) // 图像数据为空
return "";
UUID uuid = UUID.randomUUID();
String fileName = uuid+".jpg";
String path = request.getSession().getServletContext().getRealPath("upload\\"+userid+"\\"+sdf.format(date));
File targetFile = new File(path);
path = path+"\\"+fileName;
System.out.println(path);
if(!targetFile.exists()){
targetFile.mkdirs();
}
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] bytes = decoder.decodeBuffer(imgStr);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
// 生成jpeg图片
OutputStream out = new FileOutputStream(path);
out.write(bytes);
out.flush();
out.close();
return "upload/"+userid+"/"+sdf.format(date)+"/"+fileName;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
Spring MVC上传、下载 文件的更多相关文章
- spring mvc上传下载文件
前端jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...
- Spring框架学习(8)spring mvc上传下载
内容源自:spring mvc上传下载 如下示例: 页面: web.xml: <?xml version="1.0" encoding="UTF-8"?& ...
- asp.net mvc 上传下载文件的几种方式
view: <!DOCTYPE html> <html> <head> <meta name="viewport" content=&qu ...
- Spring MVC上传文件原理和resolveLazily说明
问题:使用Spring MVC上传大文件,发现从页面提交,到进入后台controller,时间很长.怀疑是文件上传完成后,才进入.由于在HTTP首部自定义了“Token”字段用于权限校验,Token的 ...
- Spring MVC上传文件
Spring MVC上传文件 1.Web.xml中加入 <servlet> <servlet-name>springmvc</servlet-name> <s ...
- Spring MVC 上传文件
Spring MVC上传文件需要如下步骤: 1.前台页面,form属性 method设置为post,enctype="multipart/form-data" input的typ ...
- rz和sz上传下载文件工具lrzsz
######################### rz和sz上传下载文件工具lrzsz ####################################################### ...
- linux上很方便的上传下载文件工具rz和sz
linux上很方便的上传下载文件工具rz和sz(本文适合linux入门的朋友) ##########################################################&l ...
- shell通过ftp实现上传/下载文件
直接代码,shell文件名为testFtptool.sh: #!/bin/bash ########################################################## ...
- .Net mvc 上传多文件
.net mvc 上传多文件有很多种方式,我的方法只是其中一种, 仅供参考,我主要是注重参数传递的过程,后面文件保存的地方省略.. 调试环境 vs2017 控制器代码: [HttpPost] publ ...
随机推荐
- springcloud分布式事务TXLCN
新增一个model,pom文件引入依赖 <dependency> <groupId>org.springframework.boot</groupId> ...
- mac下google插件SwitchyOmega笔记
端口:
- 3-MySQL高级-事务-命令(2)
事务命令 表的引擎类型必须是innodb类型才可以使用事务,这是mysql表的默认引擎 查看表的创建语句,可以看到engine=innodb -- 选择数据库 use jing_dong; -- 查看 ...
- C#枚举转化示例大全,数字或字符串转枚举
本文重点举例说明C#枚举的用法,数字转化为枚举.枚举转化为数字及其枚举数值的判断,以下是具体的示例: 先举两个简单的例子,然后再详细的举例说明: 字符串转换成枚举:DayOfWeek week=(Da ...
- 【转】tomcat系统架构分析
https://blog.csdn.net/wsl211511/article/details/51622991
- IOS中iframe的滚动条不启作用
引自:https://www.cnblogs.com/weinan/archive/2013/01/05/2832800.html 问题描述: iframe设置了高度(例如500px).倘若ifram ...
- apache的目录浏览功能 列出的文件名不完整,能否修改设置
在httpd.conf 和 httpd-vhosts.conf中的 <Directory ????> </Directory>如下设置,重启apache即可: <Dir ...
- PokerNet-poker recognition: 扑克识别 (6)
文章目录 准备 最终结果 未来改进 准备 机器: Titan XP 12GB, 64GB RAM, 机器非常强,可靠. 下次有机会购买RTX 2080 Ti 试试 最终结果 错误率可以达到万分之一,非 ...
- 透彻理解并掌握JavaScript的this
前言 无论是JavaScript新手还是老手,JavaScript中的this关键词可能都会令你困惑.本文旨在透彻地阐述this.读完本文,就再也不用怕JavaScript中的this了.你将会知道在 ...
- leetcode-219-存在重复元素②
题目描述: 第一次提交:超时 class Solution: def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool ...