当前位置:网站首页>File uploading and downloading in SSM
File uploading and downloading in SSM
2022-06-12 15:22:00 【Stars_ min】
File upload and download (maven project )
Prerequisite: import dependency
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>And then in springmvc.xml File ( The specific attribute is Baidu itself ---- I don't know , Hey )
<!-- add to multipartResolver Of bean Support file upload -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:maxUploadSize="802400" p:defaultEncoding="utf-8"/> One 、 Upload of files
stay controller Steps for uploading in :
- Get the file name :String fname = pic.getOriginalFilename();
- Process file names ( The file name stored in the database is a random file name ):
Steps to generate random names : a. Get the suffix of the file String ext = FilenameUtils.getExtension(fname); b. Generate random file names String randomName = UUID.randomUUID()+"."+ext;- Specify the physical path of the uploaded file (imgs For the generated folder ):String zPath = req.getServletContext().getRealPath("imgs");
- structure file object , Upload
Splice paths : File file = new File(zPath+'/'+randomName); FileUtils.copyInputStreamToFile(pic.getInputStream(), file); String savePath = zPath+'/'+randomName; stu.setPicPath(savePath);
Precautions for file uploading :
- In the form containing the file upload form Add to label enctype="multipart/form-data".
- Uploaded on file input In the tag type Attribute file, and name Define your own , Then the backstage is controller Need to use .
- stay controller Must be by MultipartFile pic This attribute ,pic Corresponding name attribute
Two 、 Download files
Specify the file to download :<a href="download.do?fileName=logo.jpg"> Click to download </a>
stay controller Steps for file download in :
- Get the file :String fName = req.getServletContext().getRealPath("/imgs/"+fileName);
- Build an input stream from a file :FileInputStream fis = new FileInputStream(fName);
- Building a cache :byte[] by = new byte[fis.available()]; Among them fis.available() Means to create a just right cache , modest , It is just fine
- This is optional == Prevent Chinese file names : The default Chinese operating system is GBK, The default character encoding format interpreted by the browser is ISO-8859-1
fileName = new String(fileName.getBytes("GBK"),"ISO-8859-1"); - Set the corresponding header file and related contents :
resq.setHeader("content-disposition", "attachment;filename="+fileName);
resq.setContentType("application/octet-stream");
resq.setContentLength(fis.available());Finally, perform read / write and close operations
Reading and writing
fis.read(by);// read
resq.getOutputStream().write(by);// Write
resq.getOutputStream().flush();// Refresh
resq.getOutputStream().close();// close
fis.close();This is based on ssm The file upload and download of have been completed
边栏推荐
- Leetcode daily question - fair candy bar exchange
- 使用CSDN-markdown编辑器
- ARM 64指令小记
- Browser fingerprint interpretation
- Acwing暑期每日一题(6月10日性感素数)
- Design concept of ORM framework
- 关于互联网大厂裁员
- 安装rhel 7/8 (红帽)虚拟机(转载)
- Prompt that the program cannot access key files / directories when installing PS software. Error code: 41
- 2021-06-27
猜你喜欢
![[jvm learning] class loading subsystem](/img/60/e863495ce4ea5826d1404a73c90033.jpg)
[jvm learning] class loading subsystem

Increase the maximum number of MySQL connections

SOA Architecture

Two ways of array simulating queue

PTA:自测-2 素数对猜想 (20分)

增加mysql的最大连接数

Change according to the situation, the road to promotion in the second half of 2022

Ngork implements intranet penetration -- free

Deepin20.6 rtx3080 installer le lecteur de carte graphique 510.60.02, cuda 11.6, pytorch1.11

C operator
随机推荐
Two implementation methods of generic interface
Scala download and idea installation of scala plug-ins (nanny level tutorial is super detailed)
Error 1105: message:\“raft entry is too large
Array related content
Deepin20.6 RTX3080 安裝顯卡驅動510.60.02、CUDA11.6、PyTorch1.11
Design concept of ORM framework
C operator
如何写年终总结
ROS初学者编写小乌龟以一定速度旋转一定角度的server
[jvm learning] parental delegation mechanism and PC register (program counter)
Qiming Zhixian shares the application scheme of 2.8-inch handheld central control screen
h3c GR5200路由器上如何设置公网ip可以访问
Alibaba, Tencent and pinduoduo set an example, and the new logic of industrial Internet is gradually emerging
Introduction to Eureka
粒子滤波学习记录
使用CSDN-markdown编辑器
Dart typedef的理解
Pta: self test -2 prime pair conjecture (20 points)
First set and follow set in vernacular
ROS 中 boost::bind( ) 的使用