?
Solved

Response type for an exe

Posted on 2003-03-11
4
Medium Priority
?
209 Views
Last Modified: 2010-04-01
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
Comment
Question by:tommyboy2214
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 14

Expert Comment

by:kennethxu
ID: 8113797
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
 
LVL 14

Accepted Solution

by:
kennethxu earned 800 total points
ID: 8114044
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
 

Author Comment

by:tommyboy2214
ID: 8114873
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8115773
nice script! and thanks for you point :-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
The Summer 2017 Scholarship Winners have been announced!
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question