2016-03-30 51 views
1

我在控制器中形成了一個url。當我打那個url時,我需要導出一個.txt文件。因爲我對這個概念很陌生,所以我有一個疑問,我有一個疑問,我們需要導入任何jar文件文件導出.txt文件,就像我們爲pdf和xls添加jar一樣?如何在java中下載/導出.txt文件?

我試圖像below..But我不得到它。我通過任何結果未添加任何jar文件..

FileWriter writer = new FileWriter("MyFile.txt", true); 
     writer.write("Hello World"); 
     writer.write("\r\n"); // write new line 
     writer.write("Good Bye!"); 
     writer.close(); 
+0

你的意思是你想下載該文件的權利? – androidGenX

+0

是的..我需要下載 – Bhanu

+0

你在使用servlets還是純java? – androidGenX

回答

1

在幾個項目中,我已經使用這個工具類從codejava.net

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 

/** 
* A utility that downloads a file from a URL. 
* @author www.codejava.net 
* 
*/ 
public class HttpDownloadUtility { 
    private static final int BUFFER_SIZE = 4096; 


     /** 
     * Downloads a file from a URL 
     * @param fileURL HTTP URL of the file to be downloaded 
     * @param saveDir path of the directory to save the file 
     * @throws IOException 
     */ 
     public static void downloadFile(String fileURL, String saveDir) 
       throws IOException { 
      URL url = new URL(fileURL); 
      HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); 
      int responseCode = httpConn.getResponseCode(); 

      // always check HTTP response code first 
      if (responseCode == HttpURLConnection.HTTP_OK) { 
       String fileName = ""; 
       String disposition = httpConn.getHeaderField("Content-Disposition"); 
       String contentType = httpConn.getContentType(); 
       int contentLength = httpConn.getContentLength(); 

       if (disposition != null) { 
        // extracts file name from header field 
        int index = disposition.indexOf("filename="); 
        if (index > 0) { 
         fileName = disposition.substring(index + 10, 
           disposition.length() - 1); 
        } 
       } else { 
        // extracts file name from URL 
        fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, 
          fileURL.length()); 
       } 

       System.out.println("Content-Type = " + contentType); 
       System.out.println("Content-Disposition = " + disposition); 
       System.out.println("Content-Length = " + contentLength); 
       System.out.println("fileName = " + fileName); 

       // opens input stream from the HTTP connection 
       InputStream inputStream = httpConn.getInputStream(); 
       String saveFilePath = saveDir + File.separator + fileName; 

       // opens an output stream to save into file 
       FileOutputStream outputStream = new FileOutputStream(saveFilePath); 

       int bytesRead = -1; 
       byte[] buffer = new byte[BUFFER_SIZE]; 
       while ((bytesRead = inputStream.read(buffer)) != -1) { 
        outputStream.write(buffer, 0, bytesRead); 
       } 

       outputStream.close(); 
       inputStream.close(); 

       System.out.println("File downloaded"); 
      } else { 
       System.out.println("No file to download. Server replied HTTP code: " + responseCode); 
      } 
      httpConn.disconnect(); 
     } 
    } 
+0

它下載..但是,當我們想寫入該文件的東西,該怎麼做 – Bhanu

+0

先生,你只需要一個FileWriter/PrintWriter組合:FileWriter fw = new FileWriter(fileName); PrintWriter pw = new PrintWriter(fw); ... pw.println(); –

+0

我已經回答了正確的基礎上它可以編輯,通過添加數據寫入文件的訪問 – Bhanu

1

我寫的代碼只是三行來下載.txt文件。 謝謝大家對於幫助。

我只是發佈我的答案,因爲只是下載一個空的文件誰需要爲初學者。

Adding HttpServletResponse servletResponse dependency, 

OutputStream out = servletResponse.getOutputStream(); 
String headerKey = "Content-Disposition"; 
     String headerValue = String.format("attachment; filename=\"Report"+".txt\";"); 
     servletResponse.setHeader(headerKey, headerValue); 

     // obtains response's output stream 
     OutputStream outStream = servletResponse.getOutputStream(); 

     outStream.close();