home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 November / PCONLINE_11_99.ISO / filesbbs / OS2 / APCHSSL2.ZIP / OS2HTTPD / public / htdocs / gsp / upload.gsp < prev   
Encoding:
Text File  |  1999-02-01  |  2.1 KB  |  85 lines

  1. <html>
  2. <head><title>File Upload Test</title></head>
  3. <body bgcolor="#ffffff">
  4.  
  5. <h1>File Upload Test</h1>
  6.  
  7. <java>
  8.   //
  9.   // Change this to some relative path in your doc root
  10.   // GSP must be able to write to this directory
  11.   String uploaddir = "/projects/gsp/demo/upload";
  12.  
  13.   Form form = request.getForm();
  14.   if(form.getValue("go") == null) {
  15. </java>
  16.  
  17.   <h3>GSP upload form</h3>
  18.  
  19.   Upload a .gif or .jpg:
  20.  
  21.   <form method="post" action="upload.gsp" enctype="multipart/form-data">
  22.     <input type="radio" name="ext" value=".gif">gif
  23.     <input type="radio" name="ext" value=".jpg">jpeg
  24.     <br>
  25.     <input type="file" name="file">
  26.     <input name="go" type="submit" value="Upload File">
  27.   </form>
  28.  
  29. <java>
  30.   }
  31.   else {
  32.     counter++;
  33.  
  34.     // GSP stores the file in a temp directory.  The full path to that file
  35.     // is the form value.  We will copy the file to a more permanant 
  36.     // location
  37.     File temp = new File(form.getValue("file"));
  38.     File dest = new File(request.getDocumentRoot() + uploaddir +
  39.                          File.separator +counter + form.getValue("ext"));
  40.     copyFile(temp, dest);
  41.  
  42.     out.println("Saved file to: " + dest.getAbsolutePath() + "<br>");
  43.  
  44.     out.println("<h3>Submitted form values:</h3><blockquote>");
  45.  
  46.     for(java.util.Enumeration e = request.getParameterNames();
  47.         e.hasMoreElements();) {
  48.       String key = (String)e.nextElement();
  49.       out.println(key + " = " + form.getValue(key));
  50.       out.println("<br>");
  51.     }
  52.     out.println("</blockquote>");
  53.  
  54.     out.println("<h3>Your image:</h3>");
  55.     out.println("<img src=\"" + uploaddir + "/" + counter + 
  56.                 form.getValue("ext") + "\">");
  57.   }
  58. </java>
  59.  
  60. </body>
  61. </html>
  62.  
  63.  
  64.  
  65. <java type="import">java.io.*</java>
  66. <java type="class">
  67.  
  68.   int counter = 1;
  69.  
  70.   private void copyFile(File orig, File dest) throws IOException {
  71.     int size = 4096;
  72.     BufferedInputStream is = new BufferedInputStream(new FileInputStream(orig), size);
  73.     BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(dest), size);
  74.     int b = is.read();
  75.     while(b != -1) {
  76.       os.write(b);
  77.       b = is.read();
  78.     }
  79.     is.close();
  80.     os.close();
  81.  
  82.     orig.delete();
  83.   }
  84.  
  85. </java>