導航:首頁 > 版本升級 > strtus2文件上傳代碼獲取參數

strtus2文件上傳代碼獲取參數

發布時間:2024-10-12 17:36:33

㈠ 使用struts2 怎麼實現 文件上傳到另外一台電腦

傳輸文件的功能,類似於網路聊天程序

肯定要用的文件傳輸用的IO流,還有網路專通信用到屬的socket了。

可以在你部署struts2網站的伺服器上面寫一個網路通信程序(伺服器端),對應的網路通信程序(客戶端)放在「另外一台電腦」上面。

網站的伺服器啟動之後:
1。把那個網路通信程序(伺服器端)啟動

2。把「另外一台電腦」上面的網路通信程序(客戶端)啟動,現在兩端就建立連接了。

3。可以通過伺服器端向客戶端發送數據。

以上過程跟我們平時用的聊天程序一樣。

你可以在網上看看相應的網路聊天程序,現在網上這樣的程序還是很多的。
裡面實現了這樣的機制。

㈡ 用java的三大框架實現文件的上傳下載,求代碼啊,最好是分為action,service,serv

package cn.itcast.struts2.demo1;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
* 完成文件上傳 (不是解析上傳內容,因為上傳內容 由fileUpload攔截器負責解析)
*
* @author seawind
*
*/
public class UploadAction extends ActionSupport {
// 接收上傳內容
// <input type="file" name="upload" />
private File upload; // 這里變數名 和 頁面表單元素 name 屬性一致
private String uploadContentType;
private String uploadFileName;

public void setUpload(File upload) {
this.upload = upload;
}

public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}

public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}

@Override
public String execute() throws Exception {
if (upload == null) { // 通過xml配置 required校驗器 完成校驗
// 沒有上傳文件
return NONE;
}
// 將上傳文件 保存到伺服器端
// 源文件 upload
// 目標文件
File destFile = new File(ServletActionContext.getServletContext()
.getRealPath("/upload") + "/" + uploadFileName);
// 文件復制 使用commons-io包 提供 工具
FileUtils.File(upload, destFile);
return NONE;
}
}
多文件上傳
package cn.itcast.struts2.demo1;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
* 支持多文件上傳
*
* @author seawind
*
*/
public class MultiUploadAction extends ActionSupport {
// 接收多文件上傳參數,提供數組接收就可以了
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;

public void setUpload(File[] upload) {
this.upload = upload;
}

public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}

public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}

@Override
public String execute() throws Exception {
for (int i = 0; i < upload.length; i++) {
// 循環完成上傳
File srcFile = upload[i];
String filename = uploadFileName[i];

// 定義目標文件
File destFile = new File(ServletActionContext.getServletContext()
.getRealPath("/upload" + "/" + filename));
FileUtils.File(srcFile, destFile);
}
return NONE;
}
}

㈢ 為什麼Struts2上傳文件之後擴展名變成了tmp,如何取得文件正確的擴展名

在action中獲取到上傳文件名利用上傳文件名來存儲就不會出現tmp結尾文件了

㈣ struts2上傳文件的類型去哪裡找

struts2是根據contentType來限制的,並不是文件的擴展名

比如我想僅上傳image/png,image/gif,image/jpeg這三種文件類型

第一種方法是通過javascript校驗來限制,這個比較簡單,獲取input的value然後截取擴展名進行判斷即可

第二種是根據struts2自帶的fileupload攔截器中提供的allowedTypes來進行限制,步驟如下:

1 配置fileupload攔截器

struts2的defaultStack中已經含有fileupload攔截器,如果想加入allowedTypes參數,需要從新寫一個defaultstack ,拷貝過來修改一下即可:

<interceptor-stack name="myDefaultStack">

<interceptor-ref name="exception"/>

<interceptor-ref name="alias"/>

<interceptor-ref name="servletConfig"/>

<interceptor-ref name="i18n"/>

<interceptor-ref name="prepare"/>

<interceptor-ref name="chain"/>

<interceptor-ref name="debugging"/>

<interceptor-ref name="profiling"/>

<interceptor-ref name="scopedModelDriven"/>

<interceptor-ref name="modelDriven"/>

<interceptor-ref name="fileUpload">

<param name="allowedTypes">

image/png,image/gif,image/jpeg

</param>

</interceptor-ref>

<interceptor-ref name="checkbox"/>

<interceptor-ref name="staticParams"/>

<interceptor-ref name="actionMappingParams"/>

<interceptor-ref name="params">

<param name="excludeParams">dojo\..*,^struts\..*</param>

</interceptor-ref>

<interceptor-ref name="conversionError"/>

<interceptor-ref name="validation">

<param name="excludeMethods">input,back,cancel,browse</param>

</interceptor-ref>

<interceptor-ref name="workflow">

<param name="excludeMethods">input,back,cancel,browse</param>

</interceptor-ref>

</interceptor-stack>

</interceptors>

<default-interceptor-ref name="myDefaultStack"></default-interceptor-ref>

僅修改代碼中的

<interceptor-ref name="fileUpload">

<param name="allowedTypes">

image/png,image/gif,image/jpeg

</param>

</interceptor-ref>

上面配置的是上傳文件類型的限制,其實共有兩個參數

maximumSize (可選) - 這個攔截器允許的上傳到action中的文件最大長度(以byte為單位). 注意這個參數和在webwork.properties中定義的屬性沒有關系,默認2MB

allowedTypes (可選) - 以逗號分割的contentType類型列表(例如text/html),這些列表是這個攔截器允許的可以傳到action中的contentType.如果沒有指定就是允許任何上傳類型.

2 jsp頁面定義如下(testFileUpload.jsp)

<s:form action="testFileUpload" method="post" enctype="multipart/form-data">

<s:file name="file"theme="simple"/>

<s:fielderror name="file"></s:fielderror>

<s:submit/>

</s:form>

3 後台的action聲明如下(我用的是struts2的註解進行action配置)

public class TestFileUploadAction extends ActionSupport{

private File file;

private String fileContentType;

private String fileFileName;

@Action(

value = "testFileUpload", results = {

@Result(name = "input", location = "/testFileUpload.jsp"),

@Result(name = "success", location = "/testFileUploadSuccess.jsp")

}

)

public String execute() {

return SUCCESS;

}

get/set......

}

注意:如果jsp中file的name="xxx",那麼後台action中的屬性要做相應更改為

private File xxx;

private String xxxContentType;

private String xxxFileName;

同時注意大小寫一定要一致

4 定義錯誤文件類型的消息提示,這個需要用到struts2的資源文件,在struts.properties文件中加入

struts.custom.i18n.resources=globalMessages

globalMessages對應著資源文件名

5 在源文件夾下定義資源文件globalMessages.properties,並在裡面加入如下信息:

struts.messages.error.content.type.not.allowed=upload file contenttype is invalidate

這里稍作說明(拷貝一下struts2的幫助):

如果你的action實現了ValidationAware介面(如果action繼承了ActionSupport,那麼就相當於實現了ValidationAware),這個攔截器就可以添加幾種欄位錯誤.這些錯誤信息是基於存儲在struts-messages.properties文件中的一些i18n值,這個文件是所有i18n請求的默認文件.你可以在自己消息文件的復寫以下key的消息文字

struts.messages.error.uploading - 文件不能上傳的通用錯誤信息

struts.messages.error.file.too.large - 上傳文件長度過大的錯誤信息

struts.messages.error.content.type.not.allowed - 當上傳文件不符合指定的contentType

以上配置完畢後,測試一下,對於非法的contentType,例如xxx.log這個文件的的contentType是pplication/octet-stream

會給出提示:upload file contenttype is invalidate

'.a' : 'application/octet-stream',

'.ai' : 'application/postscript',

'.aif' : 'audio/x-aiff',

'.aifc' : 'audio/x-aiff',

'.aiff' : 'audio/x-aiff',

'.au' : 'audio/basic',

'.avi' : 'video/x-msvideo',

'.bat' : 'text/plain',

'.bcpio' : 'application/x-bcpio',

'.bin' : 'application/octet-stream',

'.bmp' : 'image/x-ms-bmp',

'.c' : 'text/plain',

'.cdf' : 'application/x-cdf',

'.cdf' : 'application/x-netcdf',

'.cpio' : 'application/x-cpio',

'.csh' : 'application/x-csh',

'.css' : 'text/css',

'.dll' : 'application/octet-stream',

'.doc' : 'application/msword',

'.dot' : 'application/msword',

'.dvi' : 'application/x-dvi',

'.eml' : 'message/rfc822',

'.eps' : 'application/postscript',

'.etx' : 'text/x-setext',

'.exe' : 'application/octet-stream',

'.gif' : 'image/gif',

'.gtar' : 'application/x-gtar',

'.h' : 'text/plain',

'.hdf' : 'application/x-hdf',

'.htm' : 'text/html',

'.html' : 'text/html',

'.ief' : 'image/ief',

'.jpe' : 'image/jpeg',

'.jpeg' : 'image/jpeg',

'.jpg' : 'image/jpeg',

'.js' : 'application/x-javascript',

'.ksh' : 'text/plain',

'.latex' : 'application/x-latex',

'.m1v' : 'video/mpeg',

'.man' : 'application/x-troff-man',

'.me' : 'application/x-troff-me',

'.mht' : 'message/rfc822',

'.mhtml' : 'message/rfc822',

'.mif' : 'application/x-mif',

'.mov' : 'video/quicktime',

'.movie' : 'video/x-sgi-movie',

'.mp2' : 'audio/mpeg',

'.mp3' : 'audio/mpeg',

'.mpa' : 'video/mpeg',

'.mpe' : 'video/mpeg',

'.mpeg' : 'video/mpeg',

'.mpg' : 'video/mpeg',

'.ms' : 'application/x-troff-ms',

'.nc' : 'application/x-netcdf',

'.nws' : 'message/rfc822',

'.o' : 'application/octet-stream',

'.obj' : 'application/octet-stream',

'.oda' : 'application/oda',

'.p12' : 'application/x-pkcs12',

'.p7c' : 'application/pkcs7-mime',

'.pbm' : 'image/x-portable-bitmap',

'.pdf' : 'application/pdf',

'.pfx' : 'application/x-pkcs12',

'.pgm' : 'image/x-portable-graymap',

'.pl' : 'text/plain',

'.png' : 'image/png',

'.pnm' : 'image/x-portable-anymap',

'.pot' : 'application/vnd.ms-powerpoint',

'.ppa' : 'application/vnd.ms-powerpoint',

'.ppm' : 'image/x-portable-pixmap',

'.pps' : 'application/vnd.ms-powerpoint',

'.ppt' : 'application/vnd.ms-powerpoint',

'.ps' : 'application/postscript',

'.pwz' : 'application/vnd.ms-powerpoint',

'.py' : 'text/x-python',

'.pyc' : 'application/x-python-code',

'.pyo' : 'application/x-python-code',

'.qt' : 'video/quicktime',

'.ra' : 'audio/x-pn-realaudio',

'.ram' : 'application/x-pn-realaudio',

'.ras' : 'image/x-cmu-raster',

'.rdf' : 'application/xml',

'.rgb' : 'image/x-rgb',

'.roff' : 'application/x-troff',

'.rtx' : 'text/richtext',

'.sgm' : 'text/x-sgml',

'.sgml' : 'text/x-sgml',

'.sh' : 'application/x-sh',

'.shar' : 'application/x-shar',

'.snd' : 'audio/basic',

'.so' : 'application/octet-stream',

'.src' : 'application/x-wais-source',

'.sv4cpio': 'application/x-sv4cpio',

'.sv4crc' : 'application/x-sv4crc',

'.swf' : 'application/x-shockwave-flash',

'.t' : 'application/x-troff',

'.tar' : 'application/x-tar',

'.tcl' : 'application/x-tcl',

'.tex' : 'application/x-tex',

'.texi' : 'application/x-texinfo',

'.texinfo': 'application/x-texinfo',

'.tif' : 'image/tiff',

'.tiff' : 'image/tiff',

'.tr' : 'application/x-troff',

'.tsv' : 'text/tab-separated-values',

'.txt' : 'text/plain',

'.ustar' : 'application/x-ustar',

'.vcf' : 'text/x-vcard',

'.wav' : 'audio/x-wav',

'.wiz' : 'application/msword',

'.wsdl' : 'application/xml',

'.xbm' : 'image/x-xbitmap',

'.xlb' : 'application/vnd.ms-excel',

'.xls' : 'application/excel',

'.xls' : 'application/vnd.ms-excel',

'.xml' : 'text/xml',

'.xpdl' : 'application/xml',

'.xpm' : 'image/x-xpixmap',

'.xsl' : 'application/xml',

'.xwd' : 'image/x-xwindowmp',

'.zip' : 'application/zip',

㈤ struts2實現文件上傳的時候在form表單中設置enctype="multipart/form-data"屬性值

SmartUpload su = new SmartUpload();

su.getRequest().getParameter( );

㈥ Struts2怎麼獲取到 上傳文件的源路徑

必要前提:

a.表單method必須是post;

b.enctype取值必須是multipart/form-data;

c.提供文件選擇域。定義參數接收

File photo; // 接受上傳的文件 File接受

String photoFileName; // 上傳的文件名 文件name屬性+FileName,

String photoContentType; // 文件的MIME類型 文件name屬性+ContentType

例如:

action中

㈦ struts2 上傳過濾文件類型

在struts.xml中對於一些陌生的類型文件上傳,不太確定。我習慣在上傳的action中添加方法,判斷類型的方法。

//定義文件名稱
String fileFileName;
//獲取文件類型
String type = fileFileName.substring(fileFileName.lastIndexOf('.') + 1);
//判斷文件是否是圖片類型
if(type.equals("apk"){
裡面寫你的內容
}

閱讀全文

與strtus2文件上傳代碼獲取參數相關的資料

熱點內容
javaswt列印 瀏覽:787
js套用 瀏覽:870
缺頁中斷是屬於程序中斷 瀏覽:290
熒光棒可以編程什麼有趣的圖案 瀏覽:108
linux所有文件打包壓縮 瀏覽:196
中文系統掛載配置文件 瀏覽:508
win10要設置什麼格式文件 瀏覽:32
如何修復損壞文件 瀏覽:369
賣貨車在哪個網站 瀏覽:555
蘋果6A158 瀏覽:264
清除文件內容用什麼軟體 瀏覽:19
網上有什麼編程課 瀏覽:689
書鏈文件名是什麼 瀏覽:828
phpnginx配置文件在哪 瀏覽:601
iphone6主板焊接技術 瀏覽:140
win10何設置系統還原 瀏覽:587
word2010列印背景顏色 瀏覽:994
河北疫苗app叫什麼 瀏覽:54
win10文件夾顯示圖片 瀏覽:967
視頻太長微信上發不出怎麼辦啊 瀏覽:724

友情鏈接