This is the sample from the oreilly book. It works fine without any
adjustments.
I have not been able to get it to write to anything but the default server
directory though.
-Greg
----------------------------------------------------------------------------
----
<FORM ACTION="/servlet/UploadTest" ENCTYPE="multipart/form-data"
METHOD=POST>
What is your name? <INPUT TYPE=TEXT NAME=submitter> <BR>
Which file do you want to upload? <INPUT TYPE=FILE NAME=file> <BR>
<INPUT TYPE=SUBMIT>
</FORM>
---------------------------------------------------------------------------
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.oreilly.servlet.MultipartRequest;
public class UploadTest extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
try {
// Blindly take it on faith this is a multipart/form-data request
// Construct a MultipartRequest to help read the information.
// Pass in the request, a directory to saves files to, and the
// maximum POST size we should attempt to handle.
// Here we (rudely) write to the server root and impose 5 Meg limit.
MultipartRequest multi =
new MultipartRequest(req, ".", 5 * 1024 * 1024);
out.println("<HTML>");
out.println("<HEAD><TITLE>UploadTest</TITLE></HEAD>");
out.println("<BODY>");
out.println("<H1>UploadTest</H1>");
// Print the parameters we received
out.println("<H3>Params:</H3>");
out.println("<PRE>");
Enumeration params = multi.getParameterNames();
while (params.hasMoreElements()) {
String name = (String)params.nextElement();
String value = multi.getParameter(name);
out.println(name + " = " + value);
}
out.println("</PRE>");
// Show which files we received
out.println("<H3>Files:</H3>");
out.println("<PRE>");
Enumeration files = multi.getFileNames();
while (files.hasMoreElements()) {
String name = (String)files.nextElement();
String filename = multi.getFilesystemName(name);
String type = multi.getContentType(name);
File f = multi.getFile(name);
out.println("name: " + name);
out.println("filename: " + filename);
out.println("type: " + type);
if (f != null) {
out.println("length: " + f.length());
out.println();
}
out.println("</PRE>");
}
}
catch (Exception e) {
out.println("<PRE>");
e.printStackTrace(out);
out.println("</PRE>");
}
out.println("</BODY></HTML>");
}
}
-----Original Message-----
From: Shang Zhengjun [mailto:[EMAIL PROTECTED]]
Sent: Friday, August 18, 2000 1:01 PM
To: [EMAIL PROTECTED]
Subject: Re: Upload Image File
hi
could you show me the entire sample code of using multipartRequest
to upload images ?
thanks in advance...
On Fri, 18 Aug 2000 12:48:43 +0200
Greg Ball <[EMAIL PROTECTED]> wrote:
> Huh??,
> Why is this necessary. I am able to upload images with multipartRequest
> without adding this extra code
>
> -Greg
>
> -----Original Message-----
> From: Sundeep Goswami [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, August 17, 2000 4:19 PM
> To: [EMAIL PROTECTED]
> Subject: Re: Upload Image File
>
>
> Yes you can use the multipartRequest servlet to upload an image
> in the readNextPart() method you can check if contentType.indexOf("image")
> and do something like this
>
> // Get the blob locator and open an output stream for the blob
> theBlob =
((OracleResultSet)getBlobResult).getBLOB("image");
> blobStream = theBlob.getBinaryOutputStream();
>
> // Read a chunk of data from the sample file input stream,
> and
> write the
> // chunk into the blob column output stream. Repeat till
> file has
> been
> // fully read.
> byte[] bbuf = new byte[8 * 1024];
> int result;
> String line;
>
> // the point of this code from Hunter is to remove an
> // exraneous '\r\n' from the end of the servlet
> input stream
> boolean rnflag = false;
> while ((result = in.readLine(bbuf, 0, bbuf.length)) !=
> -1) {
>
> // Check for boundary
> if (result > 2 && bbuf[0] == '-' && bbuf[1] == '-')
{
> // quick
> pre-check
> line = new String(bbuf, 0, result, "ISO-8859-1");
> if (line.startsWith(boundary)) break;
> }
> // Are we supposed to write \r\n for the last
> iteration?
> if (rnflag) {
> blobStream.write('\r'); blobStream.write('\n');
> rnflag = false;
> }
> // Write the buffer, postpone any ending \r\n
> if (result >= 2 &&
> bbuf[result - 2] == '\r' &&
> bbuf[result - 1] == '\n') {
> blobStream.write(bbuf, 0, result - 2); // skip
the
> last 2 chars
> rnflag = true; // make a note to write them on
the
> next iteration
> }
> else {
> blobStream.write(bbuf, 0, result);
> }
> }
> System.out.println("finished writing blob field
> 'image' in table \n");
> blobStream.close();
>
>
> hope this helps
>
> >From: Gagan Goel <[EMAIL PROTECTED]>
> >Reply-To: A mailing list about Java Server Pages specification and
> > reference <[EMAIL PROTECTED]>
> >To: [EMAIL PROTECTED]
> >Subject: Upload Image File
> >Date: Thu, 17 Aug 2000 19:43:43 +0530
> >
> >Is there any way to upload an image with MultiPartRequest (or
> > somehow else)?
> >
> >Gagan
> >
>
>===========================================================================
> >To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> >JSP-INTEREST".
> >Some relevant FAQs on JSP/Servlets can be found at:
> >
> > http://java.sun.com/products/jsp/faq.html
> > http://www.esperanto.org.nz/jsp/jspfaq.html
> > http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> > http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>
> ________________________________________________________________________
> Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
>
>
===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>
>
===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets