Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to Write Contents of Jar File to Local Servlet on Tomcat 7.0.22
    primarykey
    data
    text
    <p>Good Afternoon,</p> <p>I am trying to extract the contents of a jar file programmatically and found a code snippet <a href="http://www.devx.com/tips/Tip/22124" rel="nofollow">here</a> which has allowed me to extract the contents of a jar file on my local machine without the use of a servlet. </p> <p>Now that I know the code works I am trying to use it in a local servlet environment, specifically Tomcat 7.0.22. So far I have the code below but have been unable to output the contents of the jar. I believe that I am not handling the output stream correctly and that is why nothing is saved to the local directory of my local Tomcat server. </p> <p>Would anyone be able to point me in the right direction or offer some advice on how to correct the code so that it outputs the contents read to a file?</p> <pre><code>import java.io.*; import java.util.*; import javax.servlet.*; @SuppressWarnings("serial") public class JarExtractor extends HttpServlet { @SuppressWarnings("rawtypes") protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String path = getServletContext().getRealPath("test.jar"); JarFile jar = new JarFile(path); Enumeration jarEnum = jar.entries(); while (jarEnum.hasMoreElements()) { JarEntry file = (JarEntry) jarEnum.nextElement(); File f = new File(file.getName()); if (file.isDirectory()) { // if its a directory, create it f.mkdir(); continue; } InputStream in = jar.getInputStream(file); // get the input stream OutputStream output = new FileOutputStream(f); while (in.available() &gt; 0) { // write contents of 'is' to 'fos' output.write(in.read()); } output.close(); in.close(); } } catch(Exception ex) { //------ } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { doPost(request,response); } catch (ServletException e) { // } catch (IOException e) { // } } </code></pre> <p>}</p> <p>Thank you very much for any help anyone is able to provide.</p> <p>Enjoy your weekend!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload