Results 1 to 10 of 10
  1. #1
    Kerhael is offline Member
    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Default Download file from website when using a proxy

    Hi,

    I am trying to display links on a website to allow the client to download some files hosted on the server (machine called "d1").
    The website is accessed via a proxy.

    The links display fine on the website but when I click on any of them, I got the following error message:
    "Impossible to download telecharger.do" from <Proxy IP>.
    Impossible to open this website. The requested website is unavailable or could not be found. Try again later."

    Apparently, the application looks for the file on the proxy machine and not on the "real" one, d1.

    How should I do to have the application look at the right place ?


    Here is my jsp link to "telecharger.do" to download the selected file:

    Java Code:
    <display:column property="name"  href="/reporting/telecharger.do" title="" paramId="name" paramProperty="name"/>
    And here is telecharger.do :

    Java Code:
    public class TelechargerAction extends ControleAction {
    
    	// ---------------------------- Instance Variables
    	private static Category logger = Category
    			.getInstance(TelechargerAction.class.getName());
    
    	// ---------------------------- Methods
    
    	/**
    	 * Method execute
    	 * 
    	 * @param ActionMapping
    	 *            mapping
    	 * @param ActionForm
    	 *            form
    	 * @param HttpServletRequest
    	 *            request
    	 * @param HttpServletResponse
    	 *            response
    	 * @return ActionForward
    	 * @throws Exception
    	 */
    	public ActionForward doExecute(ActionMapping mapping, ActionForm form,
    			HttpServletRequest request, HttpServletResponse response)
    			throws IOException, ServletException {
    
    		logger.debug("TelechargerAction.execute() - DEBUT -");
    		
    		ActionErrors errors = new ActionErrors();
            
    		try {
    			Properties props = PropertiesLoader.getInstance().getFile("reporting.properties");
    	    	String resDir = props.getProperty("vir.result.dir");
    			
    	    	logger.debug("resDir : " + resDir);
    	    	
    			String fileName = request.getParameter("name");
    			String filePath = resDir + "/" + fileName;
    			
    			logger.debug("filePath : " + filePath);
    			
    			// set headers
                response.setContentType("application/octet-stream");
                response.setHeader("Content-disposition", "attachment;filename=" + fileName);
    
                // Get file
                dumpFile(filePath, response.getOutputStream());
    		} catch (Throwable t) {
            	t.printStackTrace();
    			errors = new ActionErrors();
    			errors.add("user", new ActionError("erreur_interne"));
    			saveErrors(request, errors);
            	return mapping.findForward(Tokens.FAILURE);
    		}
    		
    		logger.debug("TelechargerAction.execute() - FIN -");
    		
    		return null;
    	}
    	
    	/**
         * dumps the contents of the file into the outputStream
         *
         *@param fileName fileName to dump
         *@param outputstream where to dump the file to
         */
        private void dumpFile(String fileName, OutputStream outputstream) {
        	
        	logger.debug("dumpFile() - DEBUT -");
        	
            byte[] abyte0 = new byte[4096];
    
            try {
                BufferedInputStream bufferedinputstream = new BufferedInputStream(
                        new FileInputStream(
                        fileName));
    
                while (bufferedinputstream.read(abyte0, 0, 4096) != -1) {
                    outputstream.write(abyte0, 0, 4096);
                }
    
                bufferedinputstream.close();
                outputstream.flush();
                outputstream.close();
            } catch (Exception _ex) {
            	_ex.printStackTrace();
            }
            
            logger.debug("dumpFile() - FIN -");
        } 
    }
    Regards,

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Download file from website when using a proxy

    Look again at the error: ""Impossible to download telecharger.do"

    Apparently the code is attempting to put telecharger.do to download, which of course doesn't exist as that is just a struts url mapping. I'd rather look at the code that determines what the filename for download is supposed to be, I have a strong feeling it contains an insect.

    It might also just be a vague error that I'm mis-interpreting though.
    Last edited by gimbal2; 08-06-2013 at 03:12 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Download file from website when using a proxy

    What do you see in your logs?
    Is it even ghetting to the action?
    If not then the action code is a bit pointless...

    What does the html actually look like on the browser for the link causing the problem?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Kerhael is offline Member
    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Default Re: Download file from website when using a proxy

    Hi,

    Thanks for your answers :-).

    The generated html link is the following :
    Java Code:
    a href="/wifipre/reporting/telecharger.do?name=VIR_month_20130701.csv
    And here is what is written in my logs (I deleted the beginning for more visibility):
    TelechargerAction.execute() - DEBUT -
    resDir : /var/opt/data/flat/mwpvms/files/data/vir/result
    filePath : /var/opt/data/flat/mwpvms/files/data/vir/result/VIR_month_20130701.csv
    dumpFile() - DEBUT -
    dumpFile() - FIN -
    TelechargerAction.execute() - FIN -

    The program enters telecharger.do and executes the action code, nothing goes wrong, no exception arises.

    The error message comes from the navigator, it appears in a window with "Windows Internet Explorer" as the title.

    I have just tried it on Firefox (should have tried it earlier but since we have to develop on IE, just did not think of it :-/) and the download works.

    So it is just a problem linked to Internet Explorer... But is has to work on it because our client uses only IE...

    Any idea ?

    Thanks again for your time

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Download file from website when using a proxy

    Are these always csv files?
    If so then why isn't your contentType "text/csv"?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Kerhael is offline Member
    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Default Re: Download file from website when using a proxy

    Files are always csv files, yes.
    My content type is "application/octet-stream" because I used a bit of code I found on the internet and did not change that part.
    I juste tried with "text/csv":
    Java Code:
    response.setContentType("text/csv");
    But I still got the error message

    I found here: Technical Tips and Techniques by Ravindra Gullapalli: Download problem in Internet Explorer in JSP that it might come from Internet Explorer cache but I tried the code they give with no change.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Download file from website when using a proxy

    application/octet-stream forces a download popup; if you use text/csv the data likely ends up showing in the browser window.

    Your dumpFile() method does contain a bug by the way - the read() method does not have to read 4096 bytes and your code is assuming it does. The return value of read() is the number of bytes read (or -1 if done reading), so that's the number of bytes you should output to the response. But that doesn't cause your problem, that would create a corrupted file.

    I also see that your dumpFile() method sends any exception to System.err in stead of to your log file, so if something goes wrong, you won't see that so quickly.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Download file from website when using a proxy

    text/csv will do whatever the browser is set to do (I believe).
    In the case of IE on a platform with (eg) Excel on it, it can open in-browser Excel or as an Excel app outside the browser.
    I expect you can point it at another app/or just as text in the browser, but the default is the above (from past experience).

    Oh, and it should put up the "open or save" dialog.

    Anyway, it's possible if there's duff stuff in the file (because the dumpFile() is incorrect) then IE is gagging?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Kerhael is offline Member
    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Default Re: Download file from website when using a proxy

    You are right, I corrected my dumpFile() method:
    Java Code:
            try {
                BufferedInputStream bufferedinputstream = new BufferedInputStream(
                        new FileInputStream(
                        fileName));
                
                while ((i=bufferedinputstream.read(abyte0, 0, 4096)) != -1) {
                    outputstream.write(abyte0, 0, i);
                }
                
                bufferedinputstream.close();
                outputstream.flush();
                outputstream.close();
            } catch (Exception _ex) {
            	_ex.printStackTrace();
            	logger.error("error while reading file: " + _ex.getMessage());
            }
    I solved my IE problem either :).
    What I had is a known IE issue (since IE4.0) when trying to download a file on https protocol.

    To solve it, I added following lines:

    Java Code:
    response.setHeader("Pragma", "public");
    response.setHeader("Cache-Control", "max-age=0");
    Thanks a lot to both of you !

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Download file from website when using a proxy

    Thank you too, I did not know that HTTPS problem existed.

    Note: try:

    Java Code:
    logger.error("error while reading file!", ex);
    This (should) log the stacktrace to your log file.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Download spreadsheets from a website
    By pisco in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2013, 11:09 AM
  2. Download Files From A Website
    By anthropamorphic in forum Networking
    Replies: 3
    Last Post: 10-28-2011, 03:45 AM
  3. download website source code problem
    By ahmakki in forum New To Java
    Replies: 6
    Last Post: 01-04-2011, 05:17 PM
  4. How to download data from a website.
    By bobrenjc93 in forum Java Applets
    Replies: 2
    Last Post: 05-15-2009, 07:08 AM
  5. How to download website (Get all link in website)
    By finalmem in forum Advanced Java
    Replies: 0
    Last Post: 11-12-2008, 08:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •