生活随笔
收集整理的這篇文章主要介紹了
spring mvc 实现单文件 || 多文件上传
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文件上傳 1. pom依賴(jar包) 2. 文件上傳解析器配置 3. 上傳實現(xiàn) 4. 下載||文件展示實現(xiàn)(io流的實現(xiàn))
項目下載地址
https://github.com/sevenyoungairye/File-Upload
1. pom依賴(jar包)
< dependency> < groupId> commons-fileupload
</ groupId> < artifactId> commons-fileupload
</ artifactId> < version> 1.4
</ version> </ dependency> < dependency> < groupId> commons-io
</ groupId> < artifactId> commons-io
</ artifactId> < version> 2.5
</ version> </ dependency>
2. 文件上傳解析器配置
spring-mvc.xml
< bean id = " multipartResolver" class = " org.springframework.web.multipart.commons.CommonsMultipartResolver" > < property name = " maxUploadSize" value = " 10000000" /> < property name = " maxUploadSizePerFile" value = " 2000000" /> < property name = " defaultEncoding" value = " utf-8" /> </ bean>
3. 上傳實現(xiàn)
<%--Created by IntelliJ IDEA.User: echo lovelyDate: 2020/9/5Time: 19:37文件上傳測試 demo
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html>
< head> < title> File Upload Demo..
</ title>
</ head>
< body> < form method = " post" enctype = " multipart/form-data" action = " fileUpload1" > name:
< input type = " text" name = " name" /> < br/> file:
< input type = " file" name = " fileUpload" /> < br/> < input type = " submit" value = " upload" > </ form> < br/> 多文件上傳:
< form method = " post" enctype = " multipart/form-data" action = " fileUpload2" > file1
< input type = " file" name = " uploadFiles" > < br/> file2
< input type = " file" name = " uploadFiles" > < br/> < input type = " submit" value = " upload" > </ form> </ body>
</ html>
package com
. bitqian
. controller
; import org
. springframework
. stereotype
. Controller
;
import org
. springframework
. web
. bind
. annotation
. RequestMapping
;
import org
. springframework
. web
. bind
. annotation
. ResponseBody
;
import org
. springframework
. web
. multipart
. MultipartFile
; import java
. io
. File
;
import java
. io
. IOException
;
import java
. util
. UUID
;
@Controller
public class FileUploadDemo { @RequestMapping ( value
= "/fileUpload1" ) @ResponseBody public void upload1 ( String name
, MultipartFile fileUpload
) { System
. out
. println ( name
) ; System
. out
. println ( fileUpload
) ; String originalFilename
= fileUpload
. getOriginalFilename ( ) ; try { String ext
= originalFilename
. substring ( originalFilename
. lastIndexOf ( "." ) ) ; fileUpload
. transferTo ( new File ( "f://Temp//" + UUID
. randomUUID ( ) + ext
) ) ; } catch ( IOException e
) { e
. printStackTrace ( ) ; } } @RequestMapping ( value
= "/fileUpload2" ) @ResponseBody public void uploadFile2 ( MultipartFile
[ ] uploadFiles
) { String path
= "f://temp//" ; File file
= new File ( path
) ; if ( ! file
. exists ( ) ) file
. mkdirs ( ) ; if ( uploadFiles
!= null
) { for ( MultipartFile uploadFile
: uploadFiles
) { String originalFilename
= uploadFile
. getOriginalFilename ( ) ; System
. out
. println ( "源文件名:" + originalFilename
) ; String fileName
= UUID
. randomUUID ( ) + originalFilename
. substring ( originalFilename
. lastIndexOf ( "." ) ) ; try { uploadFile
. transferTo ( new File ( path
+ fileName
) ) ; } catch ( IOException e
) { e
. printStackTrace ( ) ; } } } } }
4. 下載||文件展示實現(xiàn)(io流的實現(xiàn))
@RequestMapping ( value
= "/download/{id}" )
public void downloader ( HttpServletResponse resp
, @PathVariable ( value
= "id" ) int stuId
) { StudentInfo stu
= stuInfoService
. queryOne ( stuId
) ; String imgPath
= stu
. getImgPath ( ) ; if ( imgPath
== null
) return ; String suffix
= imgPath
. substring ( imgPath
. lastIndexOf ( "." ) ) ; resp
. setContentType ( "multipart/form-data" ) ; resp
. setHeader ( "Content-Disposition" , "attachment;fileName=" + UUID
. randomUUID ( ) + suffix
) ; FileInputStream fis
= null
; ServletOutputStream outputStream
= null
; try { fis
= new FileInputStream ( new File ( imgPath
) ) ; outputStream
= resp
. getOutputStream ( ) ; byte [ ] b
= new byte [ 1024 ] ; int read
= fis
. read ( b
) ; while ( read
!= - 1 ) { outputStream
. write ( b
, 0 , read
) ; read
= fis
. read ( b
) ; } } catch ( FileNotFoundException e
) { e
. printStackTrace ( ) ; } catch ( IOException e
) { e
. printStackTrace ( ) ; } finally { try { if ( outputStream
!= null
) outputStream
. close ( ) ; if ( fis
!= null
) fis
. close ( ) ; } catch ( IOException e
) { e
. printStackTrace ( ) ; } } }
@RequestMapping ( "/shwoImg/{id}" )
public void shwoImg ( @PathVariable ( value
= "id" ) int stuId
, HttpServletResponse resp
) { StudentInfo stu
= stuInfoService
. queryOne ( stuId
) ; String imgPath
= stu
. getImgPath ( ) ; if ( imgPath
== null
) return ; resp
. setContentType ( "image/jpeg" ) ; ServletOutputStream out
= null
; FileInputStream fis
= null
; try { out
= resp
. getOutputStream ( ) ; fis
= new FileInputStream ( new File ( imgPath
) ) ; byte [ ] b
= new byte [ 1024 ] ; int read
= fis
. read ( b
) ; while ( read
!= - 1 ) { out
. write ( b
, 0 , read
) ; read
= fis
. read ( b
) ; } } catch ( FileNotFoundException e
) { e
. printStackTrace ( ) ; } catch ( IOException e
) { e
. printStackTrace ( ) ; } finally { try { if ( fis
!= null
) fis
. close ( ) ; } catch ( IOException e
) { e
. printStackTrace ( ) ; } try { if ( out
!= null
) { out
. flush ( ) ; out
. close ( ) ; } } catch ( IOException e
) { e
. printStackTrace ( ) ; } } }
創(chuàng)作挑戰(zhàn)賽 新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎
總結(jié)
以上是生活随笔 為你收集整理的spring mvc 实现单文件 || 多文件上传 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。