如何在java中下载/导出.txt文件?

时间:2016-03-30 07:06:53

标签: java

我在控制器中形成了一个网址。当我点击该网址时,我需要导出.txt文件。由于我是这个概念的新手,我有疑问,

1)我们是否需要导入任何jar文件以导出.txt文件,就像我们为pdf和xls添加jar一样?

我试过以下..但我没有得到任何结果。我没有添加任何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();

2 个答案:

答案 0 :(得分:2)

在几个项目中,我使用了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();
        }
    }

答案 1 :(得分:2)

我写的代码只有三行才能下载.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();