當前位置:首頁 » 文件管理 » struts2上傳文件進度條

struts2上傳文件進度條

發布時間: 2022-08-19 17:01:21

A. jquery 進度條 struts2

請按照標準的W3C書寫

B. 100分。struts2實現的圖片上傳 例子

這個就是struts2的,我艹
-------------已在linux服務上線---------------------
private File userfile;
private String userfileContentType;
private String userfileFileName;
//get set 略
public void uploadImg() throws Exception {
Person person = factory.getUserServiceImpl().getCurrentPerson();//得到當前用戶
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
Calendar cal = Calendar.getInstance();
int yy = cal.get(Calendar.YEAR);
String savePath = request.getSession().getServletContext().getRealPath("/")+"/upload/images/"+yy+"/";
String filePath = savePath ;
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdirs();
}
String fileName = userfileFileName;
String basePath = "upload/images/"+yy+"/";
String uuid = UUID.randomUUID().toString();
String fileType=fileName.substring(fileName.length()-3);
File uploadedFile = new File(filePath + uuid+"."+fileType);
//userfile.write(uploadedFile);
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(userfile), 1024*1024*10);
out = new BufferedOutputStream(new FileOutputStream(uploadedFile),
1024*1024*10);
byte[] buffer = new byte[1024*1024*10];
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) {
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

response.setContentType("text/html;charaset=utf-8");
response.getWriter().write("{success:'true',fileURL:'"+basePath + uuid+"."+fileType + "'}");
}

C. struts2如何同時上傳文件以及獲得表單數據

事實上這根本不需要什麼其他配置操作,因為這是Struts2,而不是原生Servlet,在Struts2中,攔截器會將request中的表單數據(或者文件格式的數據)都和action類中的屬性名稱一一對應的注入值(包括文件數據)。所以你需要做的,其實只是在jsp頁面(或html)中加入一個file類型的input標簽,名稱記住(比如為photo),然後在action類中加一個File類型(java.io.File)欄位,此欄位必須和剛剛的input標簽name屬性一致,即photo(private File photo;)。最後,需要注意的是,當你妄圖從網頁上傳一個文件類型的表單時,必須將包圍它的form類將enctype="multipart/form-data" method="post"加上,即method必須為post,且enctype,也就是表單數據類型,必須為二進制的。

D. 關於struts2框架的文件上傳問題。。。上傳的文件超過2MB就報下面的異常,請問怎麼解決

在struts.xml中設置
<constant name="struts.multipart.maxSize" value="314572800"></constant> <!-- 允許300M -->
可以允許上傳300M的呢!我試了下,上傳了個202M的電影,竟然上傳成功了!

E. struts2文件上傳有沒有最大限制

Struts2上傳文件的默認大小限制是2M(2097152位元組),也可以通過struts.properties文件中的struts.multipart.maxSize修改,如struts.multipart.maxSize=2048 表示一次上傳文件的總大小不能超過2K位元組。

struts2上傳非常方便了,沒聽說網上有很多上傳插件,上傳核心代碼在後台,看你怎麼獲取上傳的文件數據怎麼將數據再寫入到本地伺服器。

F. struts2中文件上傳問題

你要學會用debug模式來調試自己的代碼。把斷點設置在 String root = ServletActionContext.getRequest().getRealPath("/upload"); 可以一步步查看每個變數和屬性的值 ,這樣更容易找到問題。
這里你需要查看root 和 destfile的值 這樣很容易看出你得到的路徑是否為你想要設置的路勁。
也可以用system.out.println(root) 列印到控制台看一下。

G. 我用struts2做文件上傳,傳到伺服器的始終是tmp臨時文件,我想要真正的文件,請怎麼解決

struts只能幫你上傳成文件流,你的action有個file吧,要想生成真正地文件,還得把File裝換成FileOutputStream,然後輸出到你真正的文件

H. 誰有帶進度條的文件上傳項目,java的,基於Struts2的。

flash神馬的最討厭。
請使用jquery的AJAX 那個so so easy

I. java web斷點續傳,我用的是fileupload來做的上傳。

使用Struts2上傳文件:

Struts文件上傳需要使用File Upload Filter。Filter Upload Filter使用一些默認的規則:

Form中的<s:file name="image"></s:file>標簽對應著Action類中的三個屬性分別是:上傳文件(java.io.File類型),文件名(java.lang.String類型),文件類型(java.lang.String類型,例如:image/jpeg)。命名規約為:

文件:名字與<s:file>標簽中的name屬性一致,這里為:image

文件名:文件 + FileName,這里為:imageFileName

文件類型:文件 + ContentType,這里為:imageContentType

所以針對上述<s:file name="image"></s:file>表示啊的上傳文件的JSP和Action類被別為:

imageUpload.jsp:

[html]view plain

  • <%@pagecontentType="text/html;charset=UTF-8"language="java"%>

  • <%@taglibprefix="s"uri="/struts-tags"%>

  • <html>

  • <head><title>ImageUpload</title></head>

  • <body>

  • <h1>ImageUploadPage</h1>

  • <s:formaction="imageUpload"method="post"enctype="multipart/form-data">

  • <s:filename="image"></s:file>

  • <s:submit></s:submit>

  • </s:form>

  • </body>

  • </html>



  • ImageUploadAction.java:

    [html]view plain

  • packagecom.jpleasure;

  • importcom.opensymphony.xwork2.ActionSupport;

  • importjava.io.File;

  • importjava.io.InputStream;

  • importjava.io.FileInputStream;

  • importjava.io.FileNotFoundException;

  • {

  • privateFileimage;

  • privateStringimageFileName;

  • privateStringimageContentType;

  • publicFilegetImage(){

  • returnimage;

  • }

  • publicvoidsetImage(Fileimage){

  • this.image=image;

  • }

  • publicStringgetImageFileName(){

  • returnimageFileName;

  • }

  • publicvoidsetImageFileName(StringimageFileName){

  • this.imageFileName=imageFileName;

  • }

  • (){

  • returnimageContentType;

  • }

  • publicvoidsetImageContentType(StringimageContentType){

  • this.imageContentType=imageContentType;

  • }

  • publicStringexecute(){

  • if(image!=null){

  • System.out.println("filenameis:"+this.imageFileName);

  • System.out.println("filecontenttypeis:"+this.imageContentType);

  • System.out.println("filelengthis:"+this.image.length());

  • }

  • returnSUCCESS;

  • }

  • }



  • Struts.xml配置文件:

    [html]view plain

  • <actionname="imageUpload"class="com.jpleasure.ImageUploadAction">

  • <result>/success.jsp</result>

  • </action>

  • 這樣當我們選中上傳文件,提交的時候:文件內容會以File類型的方式放在image聲明的變數中。文件的名字將會被放在imageFileName命名的變數中,文件的類型被放在imageContentType命名的變數中。

    文件下載:

    文件下載需要使用一個特殊的Result,stream類型的Result。Stream類型的Result主要用來處理文件下載操作。

    處理原理為:所有的下載文件都是將一個二進制的流寫入到HttpResponse中去。在Action類中定義一個InputSream類型的二進制流,在Result返回給用戶的時候返回給用戶。

    擴展上述的代碼,將上傳來的文件直接下載給用戶:

    ImageUploadAction中需要追加一個InputSream類型的對象,並且指向上傳的文件,代碼如下,紅色部分表示變化:

    [html]view plain

  • packagecom.jpleasure;

  • importcom.opensymphony.xwork2.ActionSupport;

  • importjava.io.File;

  • importjava.io.InputStream;

  • importjava.io.FileInputStream;

  • importjava.io.FileNotFoundException;

  • {

  • privateFileimage;

  • privateStringimageFileName;

  • privateStringimageContentType;

  • =null;

  • (){

  • returnimageInputStream;

  • }

  • publicvoidsetImageInputStream(InputStreamimageInputStream){

  • this.imageInputStream=imageInputStream;

  • }

  • publicFilegetImage(){

  • returnimage;

  • }

  • publicvoidsetImage(Fileimage){

  • this.image=image;

  • }

  • publicStringgetImageFileName(){

  • returnimageFileName;

  • }

  • publicvoidsetImageFileName(StringimageFileName){

  • this.imageFileName=imageFileName;

  • }

  • (){

  • returnimageContentType;

  • }

  • publicvoidsetImageContentType(StringimageContentType){

  • this.imageContentType=imageContentType;

  • }

  • publicStringexecute(){

  • if(image!=null){

  • System.out.println("filenameis:"+this.imageFileName);

  • System.out.println("filecontenttypeis:"+this.imageContentType);

  • System.out.println("filelengthis:"+this.image.length());

  • try{

  • this.imageInputStream=newFileInputStream(image);

  • }catch(FileNotFoundExceptione){

  • e.printStackTrace();

  • }

  • }

  • returnSUCCESS;

  • }

  • }



  • 配置文件為,紅色為變化部分:

    [html]view plain

  • <actionname="imageUpload"class="com.jpleasure.ImageUploadAction">

  • <resultname="success"type="stream">

  • <paramname="contentType">image/pjpeg</param>

  • <paramname="inputName">imageInputStream</param>

  • <paramname="contentDisposition">attachment;filename="image.jpg"</param>

  • <paramname="bufferSize">1024</param>

  • </result>

  • </action>



  • ContentType表示下載文件的類型。

    InputName表示Action類中用來下載文件的欄位的名字。

    ContentDisposition用來控制文件下載的一些信息,包括是否打開另存對話框,下載文件名等。

    BufferSize表示文件下載時使用的緩沖區的大小。

    實際項目開發的考慮:

    控制上傳文件的類型和最大允許上傳文件的size

    使用File Upload Intercepter的參數可盈控制上傳文件的類型和最大允許上傳文件的size。例如:

    [html]view plain

  • <struts>

  • <packagename="myPackage"extends="struts-default">

  • <interceptor-refname="fileUpload">

  • <paramname="maximumSize">2MB</param>

  • <paramname="allowedTypes">text/html,image/jpeg</param>

  • </interceptor-ref>

  • <interceptor-refname="basicStack"/>

  • <actionname="imageUpload"class="com.jpleasure.ImageUploadAction">

  • <resultname="success"type="stream">

  • <paramname="contentType">image/pjpeg</param>

  • <paramname="inputName">imageInputStream</param>

  • <paramname="contentDisposition">

  • attachment;filename="image.jpg"

  • </param>

  • <paramname="bufferSize">1024</param>

  • </result>

  • </action>

  • </package>

  • </struts>



  • 上述表示允許上傳jpeg和html類型的文件,且最大文件上傳size為2MB

    顯示錯誤信息:

    可以使用如下key表示的message來顯示文件上傳出錯的提示信息:

    消息Key 說明

    struts.messages.error.uploading 文件無法正常上傳時的公共錯誤

    struts.messages.error.file.too.large 文件大小超過最大允許size時的錯誤提示

    struts.messages.error.content.type.not.allowed 文件類型不在上傳文件允許類型中的錯誤提示

J. struts2 如何實現上傳整個文件夾的功能

一、壓縮文件其實是可以0壓縮率直接打包,這樣其實蠻快的
二、看到網上說Applet可以上傳文件夾,具體遠離不清楚,你可以看看
三、最笨的方法,用Ajax做一個遞歸遍歷文件夾的函數,如果是文件就上傳上去,如果是文件夾就請求後台新建文件夾
四、用JSON格式把目錄和文件封裝起來,統一傳到後台,但是後台處理要比較麻煩

熱點內容
2021款es升級了哪些配置 發布:2024-03-28 21:26:44 瀏覽:383
下述調度演算法 發布:2024-03-28 21:22:24 瀏覽:615
捷達哪個配置裝有esp 發布:2024-03-28 21:17:41 瀏覽:195
天氣源碼 發布:2024-03-28 21:14:11 瀏覽:427
使命召喚紅魔浪潮如何配置 發布:2024-03-28 21:13:08 瀏覽:545
nginx安裝php 發布:2024-03-28 21:09:47 瀏覽:666
利用python進行數據分析pdf 發布:2024-03-28 20:33:36 瀏覽:560
php模擬post提交 發布:2024-03-28 20:23:14 瀏覽:542
phptxt下載 發布:2024-03-28 20:12:37 瀏覽:476
如何更衣櫃密碼鎖密碼設置 發布:2024-03-28 19:42:09 瀏覽:484