Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

Response type for an exe

Hi all,

I am working on an application that creates a RAR self extracting exe. I would like to set the response type(content type, MIME) in the servlet
so that the application will automatically start to download(prompt user to save or run for current location). Anybody know what code sets the response header for an exe?

thanx

Tom
0
tommyboy2214
Asked:
tommyboy2214
  • 3
1 Solution
 
kennethxuCommented:
just an example:

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public final class DownloadServlet extends HttpServlet {

     public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException
     {
        String filePath = request.getPathInfo();
        String filename = request.getParameter( "filename" );
          if( filePath == null && filename != null ) filePath = "/" + filename;
          if( filename == null ) filename = filePath;
          if( filename != null ) filename = (new File(filename)).getName();

        if( filePath != null ) {
               InputStream in = null;
               OutputStream out = null;
               try {
                    in = getServletContext().getResourceAsStream(filePath);
                    if( in != null ) {
                    out = new BufferedOutputStream( response.getOutputStream() );
                    in = new BufferedInputStream( in );
                    response.setContentType( "application/unknow" );
                         response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
                    int c; while( ( c=in.read() ) != -1 ) out.write( c );
                    return;
                    }
               } finally {
                    if( in != null ) try { in.close(); } catch( Exception e ) {}
                    if( out != null ) try { out.close(); } catch( Exception e ) {}
               }
          }
          response.sendError( HttpServletResponse.SC_NOT_FOUND );
     }
}
0
 
kennethxuCommented:
based on my understanding from your previous question, this code might help you, I didn't test it myself. try it out and let me know if you have further enquires:

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public final class SFXServlet extends HttpServlet {
     private File contextDir = null;
     private File zipfileDir = null;

     public void init()
     {
          contextDir = new File( getServletContext().getRealPath( "/" ) );
          zipfileDir = new File( contextDir, "dir/zipfiles" ); // if you zipfiledir is under web root
          // zipfileDir = new File( "/path/to/dir/zipfiles" ); // you can also use absolute file system path for zipfiledir.
     }

     public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException
     {
        String filename = request.getParameter( "filename" ); // name without extension

        if( zipfileDir.exists() && filename != null ) {
               File zipfile = new File( zipfileDir, filename + ".zip" );
               InputStream in = null;
               OutputStream out = null;
               try {
                    if( zipfile.exists() ) {
                         out = new BufferedOutputStream( response.getOutputStream() );
                    response.setContentType( "application/unknow" );
                         response.setHeader("Content-Disposition","attachment; filename=\"" + filename + ".exe\"");

                         in = new BufferedInputStream( new FileInputStream( new File( contextDir, "WEB-INF/head.bin" ) ) );
                         int c;
                         while( ( c = in.read() ) != -1 ) out.write( c );
                         in.close(); in = null;

                         in = new BufferedInputStream( new FileInputStream( zipfile ) );
                         for( int ii = 0, length = (int)zipfile.length() - 2; ii < length; ii++ ) out.write( in.read() );
                         in.close(); in = null;

                         File tailer = new File( contextDir, "WEB-INF/tail.txt" );
                         long tailLength = tailer.length();
                         out.write( (int) ( tailLength & 0xFF ) );
                         out.write( (int) ( tailLength >> 8 ) );
                         in = new BufferedInputStream( new FileInputStream( tailer ) );
                         while( ( c = in.read() ) != -1 ) out.write( c );
                         in.close(); in = null;
                    }
               } finally {
                    if( in != null ) try { in.close(); } catch( Exception e ) {}
                    if( out != null ) try { out.close(); } catch( Exception e ) {}
               }
          }
          response.sendError( HttpServletResponse.SC_NOT_FOUND );
     }
}
0
 
tommyboy2214Author Commented:
thanx again for you help

just in case you're curious, i ended up using a servlet that took in parameters from checkbox and edited a script file(.sh) on  server. From there i used got a Runtime process to execute the script which basically did a cat on head.bin, zip file and tail.txt and put into an exe file. Oh, and I also needed to create a java app that edited the last two bytes on zip once it was created and which i also called from inside the script.
I tested the exe on Windows200 clients and worked perfect.
Now i just need to set response code and send back the exe to client.
thanx
0
 
kennethxuCommented:
nice script! and thanks for you point :-)
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now