SpringMVC做檔案上傳

2021-10-06 00:04:59 字數 1664 閱讀 7884

<

form

action

="upload.do"

method

="post"

enctype

="multipart/form-data"

>

檔案:<

input

type

="file"

name

="pic"

>

<

br>

<

input

type

="submit"

value

="提交"

>

<

/form

>

<

!--複雜型別表單的解析器

id:必須使用multipartresolver

-->

<

beanid=

"multipartresolver"

class

="org.springframework.web.multipart.commons.commonsmultipartresolver"

>

<

!--以位元組為單位--

>

<

property

name

="maxuploadsize"

value

="999999999999999999"

>

<

/property

>

<

/bean

>

public

class

uploadcontroller//

獲得源檔名

string

oname

=pic

.getoriginalfilename()

;//獲得檔案字尾

string

suffix

=oname

.substring

(oname

.lastindexof

("."))

;//最終檔名

filename

=filename

+suffix;/

/獲得專案部署絕對路徑

string

realpath

=req

.getsession()

.getservletcontext()

.getrealpath

("/");

bufferedoutputstreambo=

newbufferedoutputstream

(new

fileoutputstream

(realpath

+"/upload/"

+filename))

;bo.write

(bytes);

bo.flush()

;bo.close()

;return

"success";}

@("/toupload.do"

)public

string

toupload()

}

springmvc檔案上傳

上 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 param fname 檔名稱 含字尾 throws ioexception down...

spring mvc 檔案上傳

在檔案上傳時,我們需要用到檔案上傳解析器,其實,它並不陌生,只是對httpservletrequest的乙個擴充套件,使其能夠更好的處理檔案上傳,擴充套件的介面名為 org.springframework.web.multipart.multiparthttpservletrequest 先用乙個類...

Springmvc 上傳檔案

springmvc為檔案上傳提供了直接支援,通過multipartresolver實現。預設沒有裝配multipartresolver,需要先在上下文中配置multipartresolver。1 需要的jar包 commons iogroupid commons ioartifactid 2.4ve...