简体   繁体   English

通过Java桌面(非网络)中的浏览器从数据库下载文件

[英]download file from database through browser in java desktop (non web)

is it possible to download file from database through browser (ex: mozilla,chrome) in java desktop application (non web app) ? 是否可以通过Java桌面应用程序(非Web应用程序)中的浏览器(例如:mozilla,chrome)从数据库下载文件? can you explain me with an example code ? 你能用示例代码向我解释吗?
thanks in advance, 提前致谢,

Use Desktop#browse() wherein you just specify the URI of that file (exactly the one as you would enter in the address bar of a normal webbrowser). 使用Desktop#browse() ,您只需指定该文件的URI(与您在普通Web浏览器的地址栏中输入的URI完全相同)即可。

Desktop.getDesktop().browse(new URI("http://example.com/download/file.ext"));

The other side has just to set Content-Disposition: attachment on this response to force a Save As dialogue (and of course fill the response body with the necessary data from the DB). 另一边只需要在此响应上设置Content-Disposition: attachment即可强制执行“ 另存为”对话框(当然,还应使用数据库中的必要数据填充响应主体)。

Anything that is available through a browser should be available to a Java desktop app. 通过浏览器可用的任何内容都应可用于Java桌面应用程序。 At least unless the server (eg Google) goes to measures to block 'programmatic access'. 至少除非服务器(例如Google)采取措施阻止“程序访问”。

can you explain me with an example code? 您可以用示例代码向我解释吗?

Sure, adapted from the Java Sound info. 当然,改编自Java Sound信息。 page . 页面

import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;

public class LoopSound {

    public static void main(String[] args) throws Exception {
        // imagine a DB is preparing/serving this - same difference.
        URL url = new URL(
            "http://pscode.org/media/leftright.wav");
        Clip clip = AudioSystem.getClip();
        AudioInputStream ais = AudioSystem.
            getAudioInputStream( url );
        clip.open(ais);
        clip.loop(-1);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JOptionPane.showMessageDialog(null, "Close to exit!");
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM