当前位置:网站首页>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 :

  1. Get the file name :String fname = pic.getOriginalFilename();
  2. Process file names ( The file name stored in the database is a random file name ):
  3.  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;

     

  4.   Specify the physical path of the uploaded file (imgs For the generated folder ):String zPath = req.getServletContext().getRealPath("imgs");
  5. structure file object , Upload
  6.  Splice paths :
    File file = new File(zPath+'/'+randomName);
    FileUtils.copyInputStreamToFile(pic.getInputStream(), file);
    String savePath = zPath+'/'+randomName;
    stu.setPicPath(savePath);

     

Precautions for file uploading :

  1. In the form containing the file upload form Add to label enctype="multipart/form-data".
  2. Uploaded on file input In the tag type Attribute file, and name Define your own , Then the backstage is controller Need to use . 
  3. 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 :

  1. Get the file :String fName = req.getServletContext().getRealPath("/imgs/"+fileName);
  2. Build an input stream from a file :FileInputStream fis = new FileInputStream(fName);
  3. 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
  4. 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");
  5. 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

原网站

版权声明
本文为[Stars_ min]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121514598079.html