Struts2 Ajax 上傳文件,顯示進度

大家可能以前用ajax上傳文件時,是把form提交到<iframe></iframe>,HTML代碼如下:

Html代碼  收藏代碼
  1. <form action="uploadAction.action"  enctype="multipart/form-data" id="fileform" name="fileform" method="post"  
  2.      target="upload-target">  
  3. <input type="file" value="瀏覽" name="upload" />  
  4. <input type="submit" value="提交"/>  
  5. </form>  
  6. <iframe id="upload-target"></iframe>  
<form action="uploadAction.action"  enctype="multipart/form-data" id="fileform" name="fileform" method="post"
     target="upload-target">
<input type="file" value="瀏覽" name="upload" />
<input type="submit" value="提交"/>
</form>
<iframe id="upload-target"></iframe>

  這樣不用跳轉頁面就可以實現文件的上傳,Action的代碼如下:

Java代碼  收藏代碼
  1. public class UploadAction extends ActionSupport {  
  2.             //上傳的文件  
  3.     private File upload;  
  4.     //上傳文件的類型  
  5.     private String uploadContentType;  
  6.     上傳文件的文件名  
  7.     private String uploadFileName;  
  8.       
  9.       
  10.       
  11.     //getter setter  
  12.     @Override  
  13.     public String execute() throws Exception {  
  14.         System.out.println(upload.length());  
  15.         FileOutputStream fos = new FileOutputStream(ServletActionContext.getServletContext().getRealPath("/") + uploadFileName);  
  16.         FileInputStream fis = new FileInputStream(upload);  
  17.         byte[] buffer = new byte[10240];  
  18.         int len = 0;  
  19.         double temp = 0;  
  20.         int total = fis.available();  
  21.         while((len = fis.read(buffer)) > 0){  
  22.             fos.write(buffer, 0, len);  
  23.             fos.flush();  
  24.               
  25.         }  
  26.         fis.close();  
  27.         fos.close();  
  28.         return SUCCESS;  
  29.     }  
public class UploadAction extends ActionSupport {
            //上傳的文件
	private File upload;
	//上傳文件的類型
	private String uploadContentType;
	上傳文件的文件名
	private String uploadFileName;
	
	
	
	//getter setter
	@Override
	public String execute() throws Exception {
		System.out.println(upload.length());
		FileOutputStream fos = new FileOutputStream(ServletActionContext.getServletContext().getRealPath("/") + uploadFileName);
		FileInputStream fis = new FileInputStream(upload);
		byte[] buffer = new byte[10240];
		int len = 0;
		double temp = 0;
		int total = fis.available();
		while((len = fis.read(buffer)) > 0){
			fos.write(buffer, 0, len);
			fos.flush();
			
		}
		fis.close();
		fos.close();
		return SUCCESS;
	}

 

 這樣就可以在一個頁面上上傳文件了。但怎麼顯示上傳文件的進度呢。一開始我的想法是在Action增加一個perc屬性,該屬性存放着上傳的進度,再增加一個方法,輸出perc的值.

Java代碼  收藏代碼
  1.    private double perc;  
  2.   
  3. blic String ajaxGetPerc() throws Exception{  
  4.   
  5. HttpServletResponse response = ServletActionContext.getResponse();  
  6. PrintWriter writer = null;  
  7. writer = response.getWriter();  
  8. writer.println(perc);  
  9. writer.flush();  
  10. writer.close();  
  11. return null;  
     private double perc;

public String ajaxGetPerc() throws Exception{
	
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter writer = null;
		writer = response.getWriter();
		writer.println(perc);
		writer.flush();
		writer.close();
		return null;
	}

  最後試驗了一下,發現該方法行不通,能上傳文件,使用ajax返回的進度值始終是0;

  想想第二種方法,使用線程的方式上傳文件,並把進度值保存在session中, 並且要使用兩個頁面,一個頁面負責文件的上傳,一個負責顯示進度。詳細代碼如下:

upload.jsp負責文件的上傳,是一個很普通的HTML頁面

Html代碼  收藏代碼
  1. <body>  
  2. <h1>文件上傳</h1>  
  3. <form action="uploadAction.action"  enctype="multipart/form-data" id="fileform" name="fileform" method="post"  
  4.     >  
  5. <input type="file" value="瀏覽" name="upload" />  
  6. <input type="submit" value="提交"/>  
  7. </form>  
  8. </body>  
<body>
<h1>文件上傳</h1>
<form action="uploadAction.action"  enctype="multipart/form-data" id="fileform" name="fileform" method="post"
    >
<input type="file" value="瀏覽" name="upload" />
<input type="submit" value="提交"/>
</form>
</body>

  showPerc.jsp使用ajax間隔的查詢進度值

Html代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  3.     pageEncoding="UTF-8"%>  
  4. <%  
  5.     String path = request.getContextPath();  
  6.  %>  
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  8. <html xmlns="http://www.w3.org/1999/xhtml">  
  9. <head>  
  10. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  11. <script type="text/javascript" src="<%=path %>/js/jquery-1.3.2.min.js"></script>  
  12. <script type="text/javascript">  
  13. var xmlHttp ;  
  14. function createXMLHttp(){  
  15.     if(window.ActiveXObject){  
  16.         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
  17.     }  
  18.     else{  
  19.         xmlHttp = new XMLHttpRequest() ;  
  20.     }  
  21. }  
  22.   
  23. function ajaxSend(){  
  24.     createXMLHttp() ;  
  25.       
  26.     var url ="<%=path %>/getUploadPerc.action?random=" + Math.random(); ;  
  27.     xmlHttp.onreadystatechange = handler ;  
  28.     xmlHttp.open("GET",url,true) ;  
  29.     //xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
  30.     xmlHttp.send(null) ;  
  31. }  
  32. function handler(){  
  33.  if(xmlHttp.readyState == 4)     {  
  34.   if(xmlHttp.status == 200)      {  
  35.         var percent = xmlHttp.responseText ;  
  36.         document.getElementById('pre').innerHTML = percent;  
  37.         var t = setTimeout("ajaxSend()",100) ;  
  38.          
  39.         if(percent == 100){  
  40.             alert('上傳完成');  
  41.             clearTimeout(t);  
  42.         }  
  43.   }  
  44.  }  
  45.  return true;  
  46. }  
  47.   
  48. window.onload = function(){  
  49.     ajaxSend();   
  50. }  
  51. </script>  
  52. <title>文件上傳</title>  
  53. </head>  
  54. <body>  
  55. <h1>進度條</h1>  
  56. <div id="pre"></div>  
  57. </body>  
  58. </html>  
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
 %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="<%=path %>/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
var xmlHttp ;
function createXMLHttp(){
    if(window.ActiveXObject){
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
        xmlHttp = new XMLHttpRequest() ;
    }
}

function ajaxSend(){
    createXMLHttp() ;
    
    var url ="<%=path %>/getUploadPerc.action?random=" + Math.random(); ;
    xmlHttp.onreadystatechange = handler ;
    xmlHttp.open("GET",url,true) ;
    //xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlHttp.send(null) ;
}
function handler(){
 if(xmlHttp.readyState == 4)     {
  if(xmlHttp.status == 200)      {
        var percent = xmlHttp.responseText ;
        document.getElementById('pre').innerHTML = percent;
        var t = setTimeout("ajaxSend()",100) ;
       
        if(percent == 100){
            alert('上傳完成');
        	clearTimeout(t);
        }
  }
 }
 return true;
}

window.onload = function(){
	ajaxSend();	
}
</script>
<title>文件上傳</title>
</head>
<body>
<h1>進度條</h1>
<div id="pre"></div>
</body>
</html>

 Action代碼

Java代碼  收藏代碼
  1. package com.struts.action;  
  2.   
  3. import java.io.*;  
  4.   
  5. import javax.servlet.http.HttpServletResponse;  
  6. import javax.servlet.http.HttpSession;  
  7.   
  8. import org.apache.struts2.ServletActionContext;  
  9.   
  10. import com.opensymphony.xwork2.ActionSupport;  
  11.   
  12. public class UploadAction extends ActionSupport {  
  13.   
  14.     private String title;  
  15.       
  16.     private File upload;  
  17.       
  18.     private String uploadContentType;  
  19.       
  20.     private String uploadFileName;  
  21.       
  22.     private String savePath;  
  23.   
  24.     private double perc;  
  25.       
  26.     public double getPerc() {  
  27.         return perc;  
  28.     }  
  29.   
  30.     public void setPerc(double perc) {  
  31.         this.perc = perc;  
  32.     }  
  33.   
  34.     public String getTitle() {  
  35.         return title;  
  36.     }  
  37.   
  38.     public void setTitle(String title) {  
  39.         this.title = title;  
  40.     }  
  41.   
  42.     public File getUpload() {  
  43.         return upload;  
  44.     }  
  45.   
  46.     public void setUpload(File upload) {  
  47.         this.upload = upload;  
  48.     }  
  49.   
  50.     public String getUploadContentType() {  
  51.         return uploadContentType;  
  52.     }  
  53.   
  54.     public void setUploadContentType(String uploadContentType) {  
  55.         this.uploadContentType = uploadContentType;  
  56.     }  
  57.   
  58.     public String getUploadFileName() {  
  59.         return uploadFileName;  
  60.     }  
  61.   
  62.     public void setUploadFileName(String uploadFileName) {  
  63.         this.uploadFileName = uploadFileName;  
  64.     }  
  65.   
  66.     public String getSavePath() {  
  67.         return savePath;  
  68.     }  
  69.   
  70.     public void setSavePath(String savePath) {  
  71.         this.savePath = savePath;  
  72.     }  
  73.       
  74.     @Override  
  75.     public String execute() throws Exception {  
  76.         System.out.println(upload.length());  
  77.         File toFile = new File(ServletActionContext.getServletContext().getRealPath("/") + uploadFileName);  
  78.         HttpSession session = ServletActionContext.getRequest().getSession();  
  79.         session.setAttribute("perc"0);  
  80.         UploadThread uploadThread = new UploadThread(upload,toFile,session);  
  81.         Thread thread = new Thread(uploadThread);  
  82.         thread.start();  
  83.         return SUCCESS;  
  84.     }  
  85.       
  86.     public String ajaxGetPerc() throws Exception{  
  87.         HttpSession session = ServletActionContext.getRequest().getSession();  
  88.         System.out.println("session perc------" + session.getAttribute("perc"));  
  89.         //ServletActionContext.getResponse().getOutputStream().println((String)session.getAttribute("perc"));  
  90.         HttpServletResponse response = ServletActionContext.getResponse();  
  91.         PrintWriter writer = null;  
  92.         writer = response.getWriter();  
  93.         int perc = (Integer)session.getAttribute("perc");  
  94.         writer.println(perc);  
  95.         writer.flush();  
  96.         writer.close();  
  97.         return null;  
  98.     }  
  99. }  
  100.   
  101. /** 
  102.  *  
  103.  *  
  104.  * 因爲原本文件上傳完才能跳轉,故在一個線程中上傳文件,加速跳轉到showPerc.jsp去顯示進度. 
  105.  * 
  106.  */  
  107. class UploadThread implements Runnable {  
  108.     //在當前線程中上傳的文件.  
  109.     private File from ;  
  110.     //保存到服務器硬盤上的位置.  
  111.     private File to ;  
  112.     //HttpSession需要傳遞過來,直接以ServletActionContext.getRequest.getSession將會拋出NullPointerException.  
  113.     private HttpSession httpSession;  
  114.     //構造方法,用來傳址.  
  115.     public UploadThread(File from, File to , HttpSession httpSession) {  
  116.         this.from = from;  
  117.         this.to = to;  
  118.         this.httpSession = httpSession;  
  119.     }  
  120.     //線程中處理上傳.  
  121.     @Override  
  122.     public void run() {  
  123.         copy(from, to , httpSession);  
  124.     }  
  125.       
  126.     //自己實現的上傳方法,因爲需要計算進度,所以無法使用IOUtils.copy(InputStream in , OutputStream out)方法了.  
  127.     /** 
  128.      * @param from           需要上傳的文件. 
  129.      * @param to             保存到服務器上的位置. 
  130.      * @param httpSession    得到當前窗口的session. 
  131.      */  
  132.     public void copy(File from, File uploadFile , HttpSession httpSession) {  
  133.         InputStream fis;  
  134.         try {  
  135.             fis = new FileInputStream(from);  
  136.           
  137.         OutputStream fos = new FileOutputStream(uploadFile);  
  138.         byte[] buffer = new byte[10240];  
  139.         int len = 0;  
  140.         double temp = 0;  
  141.         int perc = 0;  
  142.         int total = fis.available();  
  143.         while((len = fis.read(buffer)) > 0){  
  144.             fos.write(buffer, 0, len);  
  145.             fos.flush();  
  146.             temp += len;  
  147.               
  148.             perc = (int)(temp / total * 100);  
  149.             //System.out.println(perc + "----------------" + total);  
  150.             httpSession.setAttribute("perc", perc);  
  151.             Thread.sleep(200);  
  152.         }  
  153.         fis.close();  
  154.         fos.close();  
  155.         } catch (FileNotFoundException e) {  
  156.             e.printStackTrace();  
  157.         } catch (IOException e) {  
  158.             e.printStackTrace();  
  159.         } catch (InterruptedException e) {  
  160.             e.printStackTrace();  
  161.         }  
  162.     }  
  163.       
  164. }  
package com.struts.action;

import java.io.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

	private String title;
	
	private File upload;
	
	private String uploadContentType;
	
	private String uploadFileName;
	
	private String savePath;

	private double perc;
	
	public double getPerc() {
		return perc;
	}

	public void setPerc(double perc) {
		this.perc = perc;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public File getUpload() {
		return upload;
	}

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

	public String getUploadContentType() {
		return uploadContentType;
	}

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

	public String getUploadFileName() {
		return uploadFileName;
	}

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

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	
	@Override
	public String execute() throws Exception {
		System.out.println(upload.length());
		File toFile = new File(ServletActionContext.getServletContext().getRealPath("/") + uploadFileName);
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.setAttribute("perc", 0);
		UploadThread uploadThread = new UploadThread(upload,toFile,session);
		Thread thread = new Thread(uploadThread);
		thread.start();
		return SUCCESS;
	}
	
	public String ajaxGetPerc() throws Exception{
		HttpSession session = ServletActionContext.getRequest().getSession();
		System.out.println("session perc------" + session.getAttribute("perc"));
		//ServletActionContext.getResponse().getOutputStream().println((String)session.getAttribute("perc"));
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter writer = null;
		writer = response.getWriter();
		int perc = (Integer)session.getAttribute("perc");
		writer.println(perc);
		writer.flush();
		writer.close();
		return null;
	}
}

/**
 * 
 * 
 * 因爲原本文件上傳完才能跳轉,故在一個線程中上傳文件,加速跳轉到showPerc.jsp去顯示進度.
 *
 */
class UploadThread implements Runnable {
	//在當前線程中上傳的文件.
	private File from ;
	//保存到服務器硬盤上的位置.
	private File to ;
	//HttpSession需要傳遞過來,直接以ServletActionContext.getRequest.getSession將會拋出NullPointerException.
	private HttpSession httpSession;
	//構造方法,用來傳址.
	public UploadThread(File from, File to , HttpSession httpSession) {
		this.from = from;
		this.to = to;
		this.httpSession = httpSession;
	}
	//線程中處理上傳.
	@Override
	public void run() {
		copy(from, to , httpSession);
	}
	
	//自己實現的上傳方法,因爲需要計算進度,所以無法使用IOUtils.copy(InputStream in , OutputStream out)方法了.
	/**
	 * @param from           需要上傳的文件.
	 * @param to             保存到服務器上的位置.
	 * @param httpSession    得到當前窗口的session.
	 */
	public void copy(File from, File uploadFile , HttpSession httpSession) {
		InputStream fis;
		try {
			fis = new FileInputStream(from);
		
		OutputStream fos = new FileOutputStream(uploadFile);
		byte[] buffer = new byte[10240];
		int len = 0;
		double temp = 0;
		int perc = 0;
		int total = fis.available();
		while((len = fis.read(buffer)) > 0){
			fos.write(buffer, 0, len);
			fos.flush();
			temp += len;
			
			perc = (int)(temp / total * 100);
			//System.out.println(perc + "----------------" + total);
			httpSession.setAttribute("perc", perc);
			Thread.sleep(200);
		}
		fis.close();
		fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
}

  struts.xml配置文件

Java代碼  收藏代碼
  1. <pre name="code" class="xml"> <action name="uploadAction" class="com.struts.action.UploadAction">  
  2. <result>/showloadbar.jsp</result>  
  3. <result name="input">/upload.jsp</result>  
  4. <interceptor-ref name="fileUpload">  
  5. <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>  
  6. <param name="maximumSize">20000000000</param>  
  7. </interceptor-ref>  
  8. <interceptor-ref name="defaultStack"/>  
  9. </action>  
  10. <action name="getUploadPerc" class="com.struts.action.UploadAction" method="ajaxGetPerc">  
  11. </action></pre>  
  12.    
Xml代碼  收藏代碼
  1.  <action name="uploadAction" class="com.struts.action.UploadAction">  
  2. <result>/showloadbar.jsp</result>  
  3. <result name="input">/upload.jsp</result>  
  4. <interceptor-ref name="fileUpload">  
  5. <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>  
  6. <param name="maximumSize">20000000000</param>  
  7. </interceptor-ref>  
  8. <interceptor-ref name="defaultStack"/>  
  9. </action>  
  10. <action name="getUploadPerc" class="com.struts.action.UploadAction" method="ajaxGetPerc">  
  11. </action>  
 

  在這裏我設置只能上傳圖片文件.

  可以上傳文件,並能顯示進度。但這裏使用session存取進度值,會加重服務器的負擔。各位有什麼好的方法可以告訴我。謝謝

原文鏈接:http://hzl7652.iteye.com/blog/430061

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章