2012-12-05 81 views
7

我正在使用GWT(Google Web Toolkit)創建網站。 我需要向用戶顯示一個表格,並讓用戶下載表格的內容。用戶如何在客戶端下載文件(Google Web Toolkit)

在客戶端,用戶在按下「下載」按鈕時如何下載文件?

「下載」按鈕有一個onClick()偵聽器。客戶端類延伸Composite

我試過讓這個類擴展HttpServlet,但它變得太複雜了。

我已經在這裏讀的帖子:

  1. http://www.mkyong.com/java/how-to-download-file-from-website-java-jsp/
  2. How to use GWT when downloading Files with a Servlet?

但我仍然不知道我怎麼能提供可下載的文件,以在客戶端用戶。

+0

對於客戶端:'Window.Location.replace(「/ downloadServlet」);' – Chloe

回答

21

你真的需要GWT客戶端Java代碼和服務器端Java代碼來區分。

在客戶端在你的GWT的Java代碼

String url = GWT.getModuleBaseURL() + "downloadService?fileInfo1=" + fileInfo1; 
Window.open(url, "_blank", "status=0,toolbar=0,menubar=0,location=0"); 

在服務器端在你的非Java的GWT代碼 -

在web.xml中

<servlet> 
    <servlet-name>downloadService</servlet-name> 
    <servlet-class>AAA.BBB.CCC.DownloadServlet</servlet-class> 
</servlet> 

<servlet-mapping> 
    <servlet-name>downloadService</servlet-name> 
    <url-pattern>/<gwtmodulename>/downloadService</url-pattern> 
</servlet-mapping> 

在服務器軟件包代碼中,一個servlet

public class DownloadServlet extends HttpServlet{ 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
     { 
      String fileName = req.getParameter("fileInfo1"); 

      int BUFFER = 1024 * 100; 
      resp.setContentType("application/octet-stream"); 
      resp.setHeader("Content-Disposition:", "attachment;filename=" + "\"" + fileName + "\""); 
      ServletOutputStream outputStream = resp.getOutputStream(); 
      resp.setContentLength(Long.valueOf(getfile(fileName).length()).intValue()); 
      resp.setBufferSize(BUFFER); 
      //Your IO code goes here to create a file and set to outputStream// 

     } 
    } 

確保您將文件內容推送到**outputStream**

+0

是的你是對的。我試圖做我自己的代碼,但在客戶端寫文件是不可能的。我解決了這個問題。無論如何,謝謝你的回答。 –

+0

而不是創建一個新的servlet,您可以重用您的一個GWT服務servlet,只需重寫'javax.servlet.http.HttpServlet#doGet'方法 –

+0

不太確定。你能分享一個代碼樣本嗎? – SSR

3

如果您知道文件的路徑,則代碼片段如下所示。

button.addClickHandler(new ClickHandler() 
{ 

    @Overrid 
    public void onClick(ClickEvent event) 
    { 
     Window.open(GWT.getHostPageBaseURL() + "/file.rar", "name", "enabled"); 
    } 
}); 
+0

感謝您的評論,但我已經考慮過這個問題。問題是我不能在客戶端使用文件寫入例程(它可以編譯,但會導致錯誤 - 模塊加載錯誤) –

+0

您不能在代碼上在客戶端寫入文件,這樣的東西是不允許的瀏覽器。您需要讓瀏覽器下載文件,並且用戶指定下載位置。 – Spiff

+0

你說得對。我解決這個問題的方式與此類似。無論如何,謝謝你的回答。 –

0

讀取和寫入客戶端上的文件要完成在IO部分頭號項目的答案...

你可以參考此鏈接

http://www.programcreek.com/2009/02/java-convert-a-file-to-byte-array-then-convert-byte-array-to-a-file/

或參考此代碼

File file = new File(enter the filepath here) 
FileInputStream fis = new FileInputStream(file); 
       ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
       byte[] buf = new byte[1024]; 
       try { 
        for (int readNum; (readNum = fis.read(buf)) != -1;) { 
         bos.write(buf, 0, readNum); //no doubt here is 0 
         //Writes len bytes from the specified byte array starting at offset off to this byte array output stream. 
         System.out.println("read " + readNum + " bytes,"); 
        } 
       } catch (IOException ex) { 
        System.err.println("unable to convert to bytes"); 
       } 
       byte[] bytes = bos.toByteArray(); 
       outputStream.write(bytes); 
       outputStream.flush(); 
       outputStream.close(); 

希望它有幫助!