home *** CD-ROM | disk | FTP | other *** search
- <html>
- <head><title>File Upload Test</title></head>
- <body bgcolor="#ffffff">
-
- <h1>File Upload Test</h1>
-
- <java>
- //
- // Change this to some relative path in your doc root
- // GSP must be able to write to this directory
- String uploaddir = "/projects/gsp/demo/upload";
-
- Form form = request.getForm();
- if(form.getValue("go") == null) {
- </java>
-
- <h3>GSP upload form</h3>
-
- Upload a .gif or .jpg:
-
- <form method="post" action="upload.gsp" enctype="multipart/form-data">
- <input type="radio" name="ext" value=".gif">gif
- <input type="radio" name="ext" value=".jpg">jpeg
- <br>
- <input type="file" name="file">
- <input name="go" type="submit" value="Upload File">
- </form>
-
- <java>
- }
- else {
- counter++;
-
- // GSP stores the file in a temp directory. The full path to that file
- // is the form value. We will copy the file to a more permanant
- // location
- File temp = new File(form.getValue("file"));
- File dest = new File(request.getDocumentRoot() + uploaddir +
- File.separator +counter + form.getValue("ext"));
- copyFile(temp, dest);
-
- out.println("Saved file to: " + dest.getAbsolutePath() + "<br>");
-
- out.println("<h3>Submitted form values:</h3><blockquote>");
-
- for(java.util.Enumeration e = request.getParameterNames();
- e.hasMoreElements();) {
- String key = (String)e.nextElement();
- out.println(key + " = " + form.getValue(key));
- out.println("<br>");
- }
- out.println("</blockquote>");
-
- out.println("<h3>Your image:</h3>");
- out.println("<img src=\"" + uploaddir + "/" + counter +
- form.getValue("ext") + "\">");
- }
- </java>
-
- </body>
- </html>
-
-
-
- <java type="import">java.io.*</java>
- <java type="class">
-
- int counter = 1;
-
- private void copyFile(File orig, File dest) throws IOException {
- int size = 4096;
- BufferedInputStream is = new BufferedInputStream(new FileInputStream(orig), size);
- BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(dest), size);
- int b = is.read();
- while(b != -1) {
- os.write(b);
- b = is.read();
- }
- is.close();
- os.close();
-
- orig.delete();
- }
-
- </java>