当前位置:网站首页>ssm中的文件上传和下载
ssm中的文件上传和下载
2022-06-12 15:15:00 【Stars_min】
文件的上传和下载(maven项目)
前提需要导入依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
然后在springmvc.xml文件中进行相关配置(具体属性自行百度----其实我也不知道,嘿嘿)
<!--添加multipartResolver的bean支持文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:maxUploadSize="802400" p:defaultEncoding="utf-8"/>
一、文件的上传
在controller中实现上传的步骤:
- 获取文件名:String fname = pic.getOriginalFilename();
- 对文件名进行处理(数据库中存入的文件名是随机文件名):
生成随机名的步骤: a.获取文件的后缀名 String ext = FilenameUtils.getExtension(fname); b.生成随机文件名 String randomName = UUID.randomUUID()+"."+ext;
- 指定上传文件的物理路径(imgs为生成的文件夹):String zPath = req.getServletContext().getRealPath("imgs");
- 构建file对象,实现上传
对路径进行拼接: File file = new File(zPath+'/'+randomName); FileUtils.copyInputStreamToFile(pic.getInputStream(), file); String savePath = zPath+'/'+randomName; stu.setPicPath(savePath);
文件上传注意的事项:
- 在含有文件上传的表单form标签中加入enctype="multipart/form-data"。
- 在文件上传的input标签中的type属性的file,而且name自己定义,之后的后台也就是controller需要用到。
- 在controller中必须由MultipartFile pic这个属性,pic对应name属性
二、文件的下载
指定要下载的文件:<a href="download.do?fileName=logo.jpg">点击下载</a>
在controller中实现文件下载的步骤:
- 获得文件:String fName = req.getServletContext().getRealPath("/imgs/"+fileName);
- 通过文件构建一个输入流:FileInputStream fis = new FileInputStream(fName);
- 在构建一个缓存区:byte[] by = new byte[fis.available()];其中的fis.available()表示创建一个刚刚好的缓存区,不大不小,刚刚好
- 这个可有可无==预防中文文件名:中文操作系统默认采用GBK,浏览器默认解释的字符编码格式为ISO-8859-1
fileName = new String(fileName.getBytes("GBK"),"ISO-8859-1"); - 设置相应头文件及相关内容:
resq.setHeader("content-disposition", "attachment;filename="+fileName);
resq.setContentType("application/octet-stream");
resq.setContentLength(fis.available());
最后进行读写以及关闭操作
读写
fis.read(by);//读
resq.getOutputStream().write(by);//写
resq.getOutputStream().flush();//刷新
resq.getOutputStream().close();//关闭
fis.close();
至此基于ssm的文件上传和下载已经全部搞定
边栏推荐
- C语言打开中文路径文件
- [jvm learning] virtual machine stack
- Simple crawler framework: parsing 51job page position information
- Swap numbers, XOR, operator correlation
- Use of boost:: bind() in ROS
- New features of ES6
- Qiming Zhixian shares the application scheme of 2.8-inch handheld central control screen
- ShardingSphere实践(6)——弹性伸缩
- TCP/IP 三次握手四次挥手(面试题)
- C data type
猜你喜欢
随机推荐
SQL cross database injection
Kinect2.0+ORBSLAM2_ with_ pointcloud_ map
Browser fingerprint interpretation
Deepin20.6 RTX3080 安裝顯卡驅動510.60.02、CUDA11.6、PyTorch1.11
USART(RS232422485)、I2C、SPI、CAN、USB总线
函数相关事项
Scala下载及IDEA安装Scala插件(保姆级教程超详细)
Qiming cloud sharing | demonstrate the switch through an example of the matter protocol to control the light on and off through the matter protocol
Particle filter learning record
Rust小技巧 - 通过FFI编程运行tensorrt模型
[jvm learning] parental delegation mechanism and PC register (program counter)
About layoffs in Internet companies
C常量,不能改变
C scanf function
Kinect2.0+ORBSLAM2_with_pointcloud_map
C main函数
How to write year-end summary
Function related matters
Introduction to microservices
Self induction of exception handling