I have written a java program with HttpRequest using HttpURLConnection to download/upload a file of any format(XML, Image, Documents) from a specific server using it's authentication API-KEY. This program works fine. Now i need to upload this java program file in to my website as a cloud service and need to be able to download the file from that server using this java program file which is uploaded into this website. How can i do it? One thing to make sure is the java program that i wrote is not a web application, it is just a java program.
            Asked
            
        
        
            Active
            
        
            Viewed 654 times
        
    0
            
            
        - 
                    this question has been covered under http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java please investigate in order to get your answer – oisleyen Apr 25 '16 at 07:19
- 
                    i don't want to run the program in my desktop environment. I want to run the java file in a website which then downloads a file. – Esh Val Apr 25 '16 at 07:34
1 Answers
0
            
            
        For Downloading a file Create a function like this as follows:
private void exportExcel(HttpServletResponse response, HSSFWorkbook workbook) throws CpaServiceException{
    try{
    response.reset();
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "attachment;filename=" + workbook.getSheetAt(0).getSheetName()+".xls");
    workbook.write(response.getOutputStream());
    }catch(IOException e){
        throw new CpaServiceException(CpaConstants.APPLICATION_GENERAL_ERROR);
    }
}
Then Call the function as:
@RequestMapping(value = "/"+ControllerPaths.GET_EXCEL_FOR_COMMON_PROCESS_SYSTEM_REPORT, method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public void getExcelForCommonProcessSystemReport(HttpServletRequest request, HttpServletResponse response, CpaReportFilterDTO filterDTO) throws Exception{
    exportExcel(response, reportService.generateExcelForReport(filterDTO));
}
The above code is a sample one. You can do it the similar way to download a document form the browser.
 
    
    
        srth12
        
- 873
- 9
- 16
- 
                    I wrote the whole program already and is working fine in my PC environment but now i want to run this java file in a website of mine. When i run this in that website then it should download file from another server website to my PC. This is what i'm asking for. – Esh Val Apr 25 '16 at 08:01
 
    