<%@
page
language
=
"java"
contentType
=
"text/html; charset=UTF-8"
%>
<
html
>
<
head
>
???
<
title
>
Struts2 File Upload
</
title
>
</
head
>
<
body
>
???
<
form
action
=
"fileUpload.action"
method
=
"POST"
enctype
=
"multipart/form-data"
>
???????
文件標(biāo)題:
<
input
type
=
"text"
name
=
"title"
size
=
"50"
/><
br
/>
???????
選擇文件:
<
input
type
=
"file"
name
=
"upload"
size
=
"50"
/><
br
/>
??????
<
input
type
=
"submit"
value
=
"
上傳
"
/>
???????
???
</
form
>
</
body
>
</
html
>
|
package
org.qiujy.web.struts2;
import
java.io.BufferedInputStream;
import
java.io.BufferedOutputStream;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.InputStream;
import
java.io.OutputStream;
import
org.apache.struts2.ServletActionContext;
import
com.opensymphony.xwork2.ActionSupport;
/**
?
*
@author
qiujy
?
*
@version
1.0
?
*/
public
class
FileUploadAction
extends
ActionSupport {
???
private
static
final
int
BUFFER_SIZE
= 16 * 1024;
???
//
文件標(biāo)題
???
private
String
title
;
???
//
上傳文件域?qū)ο?
???
private
File
upload
;
???
//
上傳文件名
???
private
String
uploadFileName
;
???
//
上傳文件類型
???
private
String
uploadContentType
;
???
//
保存文件的目錄路徑
(
通過依賴注入
)
???
private
String
savePath
;
???
//
以下省略
getter
和
setter......
???
//
自己封裝的一個(gè)把源文件對(duì)象復(fù)制成目標(biāo)文件對(duì)象
???
private
static
void
copy(File src, File dst) {
??????? InputStream in =
null
;
??????? OutputStream out =
null
;
???????
try
{
??????????? in =
new
BufferedInputStream(
new
FileInputStream(src),
BUFFER_SIZE
);
??????????? out =
new
BufferedOutputStream(
new
FileOutputStream(dst),
???????????????????
BUFFER_SIZE
);
???????????
byte
[] buffer =
new
byte
[
BUFFER_SIZE
];
???????????
int
len = 0;
???????????
while
((len = in.read(buffer)) > 0) {
??????????????? out.write(buffer, 0, len);
??????????? }
??????? }
catch
(Exception e) {
??????????? e.printStackTrace();
??????? }
finally
{
???????????
if
(
null
!= in) {
???????????????
try
{
??????????????????? in.close();
??????????????? }
catch
(IOException e) {
??????????????????? e.printStackTrace();
??????????????? }
??????????? }
???????????
if
(
null
!= out) {
???????????????
try
{
??????????????? ??? out.close();
??????????????? }
catch
(IOException e) {
??????????????????? e.printStackTrace();
??????????????? }
??????????? }
??????? }
??? }
???
@Override
???
public
String execute()
throws
Exception {
???????
//
根據(jù)服務(wù)器的文件保存地址和原文件名創(chuàng)建目錄文件全路徑
??????? String dstPath = ServletActionContext.
getServletContext
()
??????????????????????????????? .getRealPath(
this
.getSavePath())
??????????????????????????????? +
"\\"
+
this
.getUploadFileName();
???????
??????? System.
out
.println(
"
上傳的文件的類型:
"
+
this
.getUploadContentType());
???????
??????? File dstFile =
new
File(dstPath);
???????
copy
(
this
.
upload
, dstFile);
???????
return
SUCCESS
;
??? }
}
|
<!
DOCTYPE
struts
PUBLIC
???????
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
???????
"http://struts.apache.org/dtds/struts-2.0.dtd"
>
<
struts
>
???
<
package
name
=
"fileUploadDemo"
extends
=
"struts-default"
>
???????
<
action
name
=
"fileUpload"
class
=
"org.qiujy.web.struts2.FileUploadAction"
>
<!--
動(dòng)態(tài)設(shè)置
Action
中的
savePath
屬性的值
-->
???????????
<
param
name
=
"savePath"
>
/upload
</
param
>
???????????
<
result
name
=
"success"
>
/showupload.jsp
</
result
>
???????
</
action
>
???
</
package
>
</
struts
>
|
<!
DOCTYPE
struts
PUBLIC
???????
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
???????
"http://struts.apache.org/dtds/struts-2.0.dtd"
>
<
struts
>
???
<
constant
name
=
"struts.custom.i18n.resources"
value
=
"messages"
/>
???
<
package
name
=
"fileUploadDemo"
extends
=
"struts-default"
>
???????
<
action
name
=
"fileUpload"
???????????
class
=
"org.qiujy.web.struts2.FileUploadAction"
>
???????????
<
interceptor-ref
name
=
"fileUpload"
>
?????????????
<!--
配置允許上傳的文件類型,多個(gè)用
","
分隔
-->
?????????????
<
param
name
=
"allowedTypes"
>
???
image/bmp,image/png,image/gif,image/jpeg,image/jpg
,image/x-png, image/pjpeg
?????????????
</
param
>
?????????????
<!--
配置允許上傳的文件大小,單位字節(jié)
-->
?????????????
<
param
name
=
"maximumSize"
>
102400
</
param
>
??????????
</
interceptor-ref
>
??????????
<
interceptor-ref
name
=
"defaultStack"
/>
???????????
<!--
動(dòng)態(tài)設(shè)置
Action
中的
savePath
屬性的值
-->
???????????
<
param
name
=
"savePath"
>
/upload
</
param
>
???????????
<
result
name
=
"input"
>
/index.jsp
</
result
>
???????????
<
result
name
=
"success"
>
/showupload.jsp
</
result
>
???????
</
action
>
???
</
package
>
</
struts
>
|
#
更改上傳文件類型不允許的提示信息
struts.messages.error.content.type.not.allowed=
文件上傳
失敗:你要上傳的文件類型不允許
#
更改上傳文件太大的提示信息
struts.messages.error.file.too.large=
文件上傳
失敗:你要上傳的文件太大
#
文件上傳
其它錯(cuò)誤信息
struts.messages.error.uploading=
文件上傳
失敗:發(fā)生內(nèi)部錯(cuò)誤
|
Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir
Removing file upload D:\tomcat6.0.13\work\Catalina\localhost\fileload_struts2\upload__4b616fd1_115a3d5d9dc__7fff_00000005.tmp
|
<%@
page
language
=
"java"
contentType
=
"text/html; charset=UTF-8"
%>
<%@
taglib
uri
=
"/struts-tags"
prefix
=
"s"
%>
<
html
>
<
head
>
</
head
>
<
body
>
???
<
font
color
=
"red"
><
s:fielderror
/></
font
>
???
<
form
action
=
"multiFileUpload.action"
method
=
"POST"
enctype
=
"multipart/fo
發(fā)表評(píng)論
最近訪客 更多訪客>>最新評(píng)論
|
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

評(píng)論