0

我想将文件从服务器下载到客户端计算机。但我希望从浏览器下载文件:我希望将文件保存在下载文件夹中。

我使用以下代码下载文件。

public void descarga(String address, String localFileName) {

OutputStream out = null;
URLConnection conn = null;
InputStream in = null;
try {
    // Get the URL
    URL url = new URL(address);
    // Open an output stream to the destination file on our local filesystem

    out = new BufferedOutputStream(new FileOutputStream(localFileName));
    conn = url.openConnection();
    in = conn.getInputStream();

    // Get the data
    byte[] buffer = new byte[1024];
    int numRead;
    while ((numRead = in.read(buffer)) != -1) {
        out.write(buffer, 0, numRead);
    }
    // Done! Just clean up and get out
} catch (Exception exception) {
    exception.printStackTrace();
} finally {
    try {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    } catch (IOException ioe) {
        // Shouldn't happen, maybe add some logging here if you are not
        // fooling around ;)
    }
    }

它可以工作,但除非我指定绝对路径,否则它不会下载文件,因此从具有不同浏览器的不同客户端使用是无用的,因为网页甚至不会提示让用户知道正在下载文件的消息。我可以添加什么来让它工作?

谢谢

4

0 回答 0