开发者

Create downloadable file with URL

开发者 https://www.devze.com 2023-02-07 03:18 出处:网络
How can i make a local file on any computer available as a downloadable file with a URL. The URL would be accessed by a client java application to down开发者_如何学编程load the file.You can use Jetty

How can i make a local file on any computer available as a downloadable file with a URL. The URL would be accessed by a client java application to down开发者_如何学编程load the file.


You can use Jetty server. It's very easy to embed it in your Java SE application. You can find more info here:

http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty

I think section Configuring a File Server would be especially interesting for you:

http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty#Configuring_a_File_Server

Here is an example that you can find there:

public class FileServer
{
    public static void main(String[] args) throws Exception
    {
        Server server = new Server();
        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setPort(8080);
        server.addConnector(connector);

        ResourceHandler resource_handler = new ResourceHandler();
        resource_handler.setDirectoriesListed(true);
        resource_handler.setWelcomeFiles(new String[]{ "index.html" });

        resource_handler.setResourceBase(".");

        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
        server.setHandler(handlers);

        server.start();
        server.join();
    }
}

If you configure resourceBase with path to your folder like this: resource_handler.setResourceBase("/path/to/your/folder"), then all files in this folder would be available through HTTP.


Since Java 1.6, there's a built-in HTTP server included with the JDK.

This code is used to setup a simple http server to get a specific PDF (with the request http://localhost:8000/get)

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.Headers;

public class SimpleHttpServer {

  public static void main(String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
    server.createContext("/info", new InfoHandler());
    server.createContext("/get", new GetHandler());
    server.setExecutor(null); // creates a default executor
    server.start();
  }

  static class InfoHandler implements HttpHandler {
    public void handle(HttpExchange t) throws IOException {
      String response = "Use /get to download a PDF";
      t.sendResponseHeaders(200, response.length());
      OutputStream os = t.getResponseBody();
      os.write(response.getBytes());
      os.close();
    }
  }

  static class GetHandler implements HttpHandler {
    public void handle(HttpExchange t) throws IOException {

      // add the required response header for a PDF file
      Headers h = t.getResponseHeaders();
      h.add("Content-Type", "application/pdf");

      // a PDF (you provide your own!)
      File file = new File ("c:/temp/doc.pdf");
      byte [] bytearray  = new byte [(int)file.length()];
      FileInputStream fis = new FileInputStream(file);
      BufferedInputStream bis = new BufferedInputStream(fis);
      bis.read(bytearray, 0, bytearray.length);

      // ok, we are ready to send the response.
      t.sendResponseHeaders(200, file.length());
      OutputStream os = t.getResponseBody();
      os.write(bytearray,0,bytearray.length);
      os.close();
    }
  }
}

ref : http://www.rgagnon.com/javadetails/java-have-a-simple-http-server.html


Create a Java server, be sure to expose it if you're behind a router/proxy/firewall if you're accessing it externally from the internet, if not, if you're in the same subnet then it's fine ignore this consideration.

On the Java server, make it listen to a specific port and handle TCP calls from a custom Java client on this predetermined agreed port to exchange data. Then get the Java server to use a System.IO call or similar to serve out the file as a byte stream to the client.

The Java server can preferably be say LAMP or Jetty or any minified Java-based server actually, so you needn't begin from scratch.

Hope this helps.


You can set up a webserver.

A Java webserver:

  • Oracle Networking (Code) and this is the project page: project page

Of course, if you are behind a router, you will need to forward port 80 to your computer.

0

精彩评论

暂无评论...
验证码 换一张
取 消