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 ;)
    }
    }

それは機能しますが、絶対パスを指定しない限りファイルをダウンロードしないため、Webページはファイルがダウンロードされていることをユーザーに知らせるメッセージさえ表示しないため、異なるブラウザーを使用する異なるクライアントから使用しても意味がありません. それを機能させるために何を追加できますか?

ありがとう

4

0 に答える 0