-3

I want to add a scheduling job to cron tab which is like GET request with a url. I wrote following code in my restful application for that endpoint to perform downloads of some xml files from given URLs. I found that it requires user interaction to select save or open from a pop up box. I want the job to be automated without any user interaction from the cron scheduler.

import java.io.File;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

@Path("/metadata-management")
public class FileService {

private static final String FILE_PATH = "c:\\file.log";

@GET
@Path("/get")
@Produces("text/plain")
public Response getFile() {

    File file = new File(FILE_PATH);

    ResponseBuilder response = Response.ok((Object) file);
    response.header("Content-Disposition",
        "attachment; filename=\"file_from_server.log\"");
    return response.build();

}

}

Should I just use HttpURLConnection to download file from an HTTP URL instead to download file within the above code block and return null for response?

  • *how should I write my code?* I reccommend you a keyboard... – Jordi Castilla May 20 '15 at 13:48
  • Your question is too broad - There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow down the answer set or to isolate an issue that can be answered in a few paragraphs. – Kmeixner May 20 '15 at 13:59
  • Thanks. I added more context with my code. – user3157008 May 20 '15 at 14:20

2 Answers2

0

try this:

public static String loadXml(String url) {
     HttpsURLConnection con = (HttpsURLConnection) new URL(url).openConnection();
     System.setProperty("http.agent", "");
     con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
     con.setRequestMethod("GET");
     con.setDoInput(true);
     con.setDoOutput(true);
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     int n;
     while (( n =con.getInputStream().read())!=-1) {
         baos.write(n);
     };

     con.disconnect();
     return new String(baos.toByteArray(),"UTF-8");
}
Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
0

Using the URL.getInputStream() method, you can create a BufferedReader if you want, and read all of the data to a string. Then, use a BufferedWriter to write it to a file. Use this code:

public void copy(URL source, File destination) throws Exception {
    InputStream stream = source.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
    StringBuffer data = new StringBuffer("");
    String line;
    while((line = br.readLine()) != null) {
        data.append(line + System.lineSeparator());
    }
    br.close();
    BufferedWriter bw = new BufferedWriter(new FileWriter(destination));
    bw.write(data.toString());
    bw.close();
}
hyper-neutrino
  • 5,272
  • 2
  • 29
  • 50