participate


Java Programming [Archive] - Useful Code of the Day: Multipart Form File Upload
<<   Back to Forum  |   Give us Feedback
This topic has 19 replies on 2 pages.    1 | 2 | Next »
bsampieri
Posts:14,508
Registered: 02/04/99
Useful Code of the Day: Multipart Form File Upload   
Oct 3, 2003 6:10 PM

 
So, you want to upload files to your web server, do ya? Well, I've seen this topic posted a few times in the last month or so and many of the response I've seen here haven't included definitive answers. Of course, the actual problems vary, but ultimately, a bunch of people want to do file upload from an applet or application to an existing file upload script (CGI, PHP, JSP, etc.) on a web server. And invariably, there are problems with formatting the HTTP request to get things working.

Well, I had a need to do the same thing. I realize there are other solutions out there, such as some sample code that comes with Jakarta Commons Upload. But since we all like reusable code, and we also all seem to like reinventing wheels, here's my go at it: MultiPartFormOutputStream!

MultiPartFormOutputStream is a specialized OutputStream-like class. You create your URLConnection to the server (a static method included can do this for a URL for you, as some of the settings, doInput and doOutput specifically, seem to confuse people). Then get the OutputStream from it and create a boundary string (a static method to create one is provided as well) and pass them to the constructor. Now you have a MultiPartFormOutputStream which you can use to write form fields like text fields, checkboxes, etc., as well as write file data (from Files, InputStreams or raw bytes).

There are some convenience methods for writing primative type values as well as strings, but any higher level objects (aside from Files or InputStreams) aren't supported. (You can always serialize and pass the raw bytes.)

Sample usage code is below. Also, any recommendations for improvement are requested. The code was tested with the Jakarta Struts.

import java.io.*;
import java.net.*;
 
/**
 * <code>MultiPartFormOutputStream</code> is used to write 
 * "multipart/form-data" to a <code>java.net.URLConnection</code> for 
 * POSTing.  This is primarily for file uploading to HTTP servers.  
 * 
 * @since  JDK1.3
 */
public class MultiPartFormOutputStream {
	/**
	 * The line end characters.  
	 */
	private static final String NEWLINE = "\r\n";
 
	/**
	 * The boundary prefix.  
	 */
	private static final String PREFIX = "--";
 
	/**
	 * The output stream to write to.  
	 */
	private DataOutputStream out = null;
 
	/**
	 * The multipart boundary string.  
	 */
	private String boundary = null;
 
	/**
	 * Creates a new <code>MultiPartFormOutputStream</code> object using 
	 * the specified output stream and boundary.  The boundary is required 
	 * to be created before using this method, as described in the 
	 * description for the <code>getContentType(String)</code> method.  
	 * The boundary is only checked for <code>null</code> or empty string, 
	 * but it is recommended to be at least 6 characters.  (Or use the 
	 * static createBoundary() method to create one.)
	 * 
	 * @param  os        the output stream
	 * @param  boundary  the boundary
	 * @see  #createBoundary()
	 * @see  #getContentType(String)
	 */
	public MultiPartFormOutputStream(OutputStream os, String boundary) {
		if(os == null) {
			throw new IllegalArgumentException("Output stream is required.");
		}
		if(boundary == null || boundary.length() == 0) {
			throw new IllegalArgumentException("Boundary stream is required.");
		}
		this.out = new DataOutputStream(os);
		this.boundary = boundary;
	}
 
	/**
	 * Writes an boolean field value.  
	 * 
	 * @param  name   the field name (required)
	 * @param  value  the field value
	 * @throws  java.io.IOException  on input/output errors
	 */
	public void writeField(String name, boolean value) 
			throws java.io.IOException {
		writeField(name, new Boolean(value).toString());
	}
 
	/**
	 * Writes an double field value.  
	 * 
	 * @param  name   the field name (required)
	 * @param  value  the field value
	 * @throws  java.io.IOException  on input/output errors
	 */
	public void writeField(String name, double value) 
			throws java.io.IOException {
		writeField(name, Double.toString(value));
	}
 
	/**
	 * Writes an float field value.  
	 * 
	 * @param  name   the field name (required)
	 * @param  value  the field value
	 * @throws  java.io.IOException  on input/output errors
	 */
	public void writeField(String name, float value) 
			throws java.io.IOException {
		writeField(name, Float.toString(value));
	}
 
	/**
	 * Writes an long field value.  
	 * 
	 * @param  name   the field name (required)
	 * @param  value  the field value
	 * @throws  java.io.IOException  on input/output errors
	 */
	public void writeField(String name, long value) 
			throws java.io.IOException {
		writeField(name, Long.toString(value));
	}
 
	/**
	 * Writes an int field value.  
	 * 
	 * @param  name   the field name (required)
	 * @param  value  the field value
	 * @throws  java.io.IOException  on input/output errors
	 */
	public void writeField(String name, int value) 
			throws java.io.IOException {
		writeField(name, Integer.toString(value));
	}
 
	/**
	 * Writes an short field value.  
	 * 
	 * @param  name   the field name (required)
	 * @param  value  the field value
	 * @throws  java.io.IOException  on input/output errors
	 */
	public void writeField(String name, short value) 
			throws java.io.IOException {
		writeField(name, Short.toString(value));
	}
 
	/**
	 * Writes an char field value.  
	 * 
	 * @param  name   the field name (required)
	 * @param  value  the field value
	 * @throws  java.io.IOException  on input/output errors
	 */
	public void writeField(String name, char value) 
			throws java.io.IOException {
		writeField(name, new Character(value).toString());
	}
 
	/**
	 * Writes an string field value.  If the value is null, an empty string 
	 * is sent ("").  
	 * 
	 * @param  name   the field name (required)
	 * @param  value  the field value
	 * @throws  java.io.IOException  on input/output errors
	 */
	public void writeField(String name, String value) 
			throws java.io.IOException {
		if(name == null) {
			throw new IllegalArgumentException("Name cannot be null or empty.");
		}
		if(value == null) {
			value = "";
		}
		/*
		--boundary\r\n
		Content-Disposition: form-data; name="<fieldName>"\r\n
		\r\n
		<value>\r\n
		*/
		// write boundary
		out.writeBytes(PREFIX);
		out.writeBytes(boundary);
		out.writeBytes(NEWLINE);
		// write content header
		out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"");
		out.writeBytes(NEWLINE);
		out.writeBytes(NEWLINE);
		// write content
		out.writeBytes(value);
		out.writeBytes(NEWLINE);
		out.flush();
	}
 
	/**
	 * Writes a file's contents.  If the file is null, does not exists, or 
	 * is a directory, a <code>java.lang.IllegalArgumentException</code> 
	 * will be thrown.  
	 * 
	 * @param  name      the field name
	 * @param  mimeType  the file content type (optional, recommended)
	 * @param  file      the file (the file must exist)
	 * @throws  java.io.IOException  on input/output errors
	 */
	public void writeFile(String name, String mimeType, File file) 
			throws java.io.IOException {
		if(file == null) {
			throw new IllegalArgumentException("File cannot be null.");
		}
		if(!file.exists()) {
			throw new IllegalArgumentException("File does not exist.");
		}
		if(file.isDirectory()) {
			throw new IllegalArgumentException("File cannot be a directory.");
		}
		writeFile(name, mimeType, file.getCanonicalPath(), new FileInputStream(file));
	}
 
	/**
	 * Writes a input stream's contents.  If the input stream is null, a 
	 * <code>java.lang.IllegalArgumentException</code> will be thrown.  
	 * 
	 * @param  name      the field name
	 * @param  mimeType  the file content type (optional, recommended)
	 * @param  fileName  the file name (required)
	 * @param  is        the input stream
	 * @throws  java.io.IOException  on input/output errors
	 */
	public void writeFile(String name, String mimeType, 
			String fileName, InputStream is) 
			throws java.io.IOException {
		if(is == null) {
			throw new IllegalArgumentException("Input stream cannot be null.");
		}
		if(fileName == null || fileName.length() == 0) {
			throw new IllegalArgumentException("File name cannot be null or empty.");
		}
		/*
		--boundary\r\n
		Content-Disposition: form-data; name="<fieldName>"; filename="<filename>"\r\n
		Content-Type: <mime-type>\r\n
		\r\n
		<file-data>\r\n
		*/
		// write boundary
		out.writeBytes(PREFIX);
		out.writeBytes(boundary);
		out.writeBytes(NEWLINE);
		// write content header
		out.writeBytes("Content-Disposition: form-data; name=\"" + name + 
			"\"; filename=\"" + fileName + "\"");
		out.writeBytes(NEWLINE);
		if(mimeType != null) {
			out.writeBytes("Content-Type: " + mimeType);
			out.writeBytes(NEWLINE);
		}
		out.writeBytes(NEWLINE);
		// write content
		byte[] data = new byte[1024];
		int r = 0;
		while((r = is.read(data, 0, data.length)) != -1) {
			out.write(data, 0, r);
		}
		// close input stream, but ignore any possible exception for it
		try {
			is.close();
		} catch(Exception e) {}
		out.writeBytes(NEWLINE);
		out.flush();
	}
 
	/**
	 * Writes the given bytes.  The bytes are assumed to be the contents 
	 * of a file, and will be sent as such.  If the data is null, a 
	 * <code>java.lang.IllegalArgumentException</code> will be thrown.  
	 * 
	 * @param  name      the field name
	 * @param  mimeType  the file content type (optional, recommended)
	 * @param  fileName  the file name (required)
	 * @param  data      the file data
	 * @throws  java.io.IOException  on input/output errors
	 */
	public void writeFile(String name, String mimeType, 
			String fileName, byte[] data) 
			throws java.io.IOException {
		if(data == null) {
			throw new IllegalArgumentException("Data cannot be null.");
		}
		if(fileName == null || fileName.length() == 0) {
			throw new IllegalArgumentException("File name cannot be null or empty.");
		}
		/*
		--boundary\r\n
		Content-Disposition: form-data; name="<fieldName>"; filename="<filename>"\r\n
		Content-Type: <mime-type>\r\n
		\r\n
		<file-data>\r\n
		*/
		// write boundary
		out.writeBytes(PREFIX);
		out.writeBytes(boundary);
		out.writeBytes(NEWLINE);
		// write content header
		out.writeBytes("Content-Disposition: form-data; name=\"" + name + 
			"\"; filename=\"" + fileName + "\"");
		out.writeBytes(NEWLINE);
		if(mimeType != null) {
			out.writeBytes("Content-Type: " + mimeType);
			out.writeBytes(NEWLINE);
		}
		out.writeBytes(NEWLINE);
		// write content
		out.write(data, 0, data.length);
		out.writeBytes(NEWLINE);
		out.flush();
	}
 
	/**
	 * Flushes the stream.  Actually, this method does nothing, as the only 
	 * write methods are highly specialized and automatically flush.  
	 * 
	 * @throws  java.io.IOException  on input/output errors
	 */
	public void flush() throws java.io.IOException {
		// out.flush();
	}
 
	/**
	 * Closes the stream.  <br />
	 * <br />
	 * <b>NOTE:</b> This method <b>MUST</b> be called to finalize the 
	 * multipart stream.
	 * 
	 * @throws  java.io.IOException  on input/output errors
	 */
	public void close() throws java.io.IOException {
		// write final boundary
		out.writeBytes(PREFIX);
		out.writeBytes(boundary);
		out.writeBytes(PREFIX);
		out.writeBytes(NEWLINE);
		out.flush();
		out.close();
	}
 
	/**
	 * Gets the multipart boundary string being used by this stream.  
	 * 
	 * @return  the boundary
	 */
	public String getBoundary() {
		return this.boundary;
	}
 
	/**
	 * Creates a new <code>java.net.URLConnection</code> object from the 
	 * specified <code>java.net.URL</code>.  This is a convenience method 
	 * which will set the <code>doInput</code>, <code>doOutput</code>, 
	 * <code>useCaches</code> and <code>defaultUseCaches</code> fields to 
	 * the appropriate settings in the correct order.  
	 * 
	 * @return  a <code>java.net.URLConnection</code> object for the URL
	 * @throws  java.io.IOException  on input/output errors
	 */
	public static URLConnection createConnection(URL url) 
			throws java.io.IOException {
		URLConnection urlConn = url.openConnection();
		if(urlConn instanceof HttpURLConnection) {
			HttpURLConnection httpConn = (HttpURLConnection)urlConn;
			httpConn.setRequestMethod("POST");
		}
		urlConn.setDoInput(true);
		urlConn.setDoOutput(true);
		urlConn.setUseCaches(false);
		urlConn.setDefaultUseCaches(false);
		return urlConn;
	}
 
	/**
	 * Creates a multipart boundary string by concatenating 20 hyphens (-) 
	 * and the hexadecimal (base-16) representation of the current time in 
	 * milliseconds.  
	 * 
	 * @return  a multipart boundary string
	 * @see  #getContentType(String)
	 */
	public static String createBoundary() {
		return "--------------------" + 
			Long.toString(System.currentTimeMillis(), 16);
	}
 
	/**
	 * Gets the content type string suitable for the 
	 * <code>java.net.URLConnection</code> which includes the multipart 
	 * boundary string.  <br />
	 * <br />
	 * This method is static because, due to the nature of the 
	 * <code>java.net.URLConnection</code> class, once the output stream 
	 * for the connection is acquired, it's too late to set the content 
	 * type (or any other request parameter).  So one has to create a 
	 * multipart boundary string first before using this class, such as 
	 * with the <code>createBoundary()</code> method.  
	 * 
	 * @param  boundary  the boundary string
	 * @return  the content type string
	 * @see  #createBoundary()
	 */
	public static String getContentType(String boundary) {
		return "multipart/form-data; boundary=" + boundary;
	}
}


Usage: (try/catch left out to shorten the post a bit)
URL url = new URL("http://www.domain.com/webems/upload.do");
// create a boundary string
String boundary = MultiPartFormOutputStream.createBoundary();
URLConnection urlConn = MultiPartFormOutputStream.createConnection(url);
urlConn.setRequestProperty("Accept", "*/*");
urlConn.setRequestProperty("Content-Type", 
	MultiPartFormOutputStream.getContentType(boundary));
// set some other request headers...
urlConn.setRequestProperty("Connection", "Keep-Alive");
urlConn.setRequestProperty("Cache-Control", "no-cache");
// no need to connect cuz getOutputStream() does it
MultiPartFormOutputStream out = 
	new MultiPartFormOutputStream(urlConn.getOutputStream(), boundary);
// write a text field element
out.writeField("myText", "text field text");
// upload a file
out.writeFile("myFile", "text/plain", new File("C:\\test.txt"));
// can also write bytes directly
//out.writeFile("myFile", "text/plain", "C:\\test.txt", 
//	"This is some file text.".getBytes("ASCII"));
out.close();
// read response from server
BufferedReader in = new BufferedReader(
	new InputStreamReader(urlConn.getInputStream()));
String line = "";
while((line = in.readLine()) != null) {
	 System.out.println(line);
}
in.close();


"Useful Code of the Day" is supplied by the person who posted this message. This code is not guaranteed by any warranty whatsoever. The code is free to use and modify as you see fit. The code was tested and worked for the author. If anyone else has some useful code, feel free to post it under this heading.
 
DigitalDreamer
Posts:513
Registered: 4/28/03
Re: Useful Code of the Day: Multipart Form File Upload   
Oct 3, 2003 7:44 PM (reply 1 of 19)  (In reply to original post )

 
"Useful Code of the Day" is supplied by the person who
posted this message. This code is not guaranteed by
any warranty whatsoever. The code is free to use and
modify as you see fit. The code was tested and worked
for the author. If anyone else has some useful code,
feel free to post it under this heading.

My code comes with a free 16 day money back garuntee. This does not effect your statutory rights.
 
bsampieri
Posts:14,508
Registered: 02/04/99
Re: Useful Code of the Day: Multipart Form File Upload   
Oct 6, 2003 7:17 AM (reply 2 of 19)  (In reply to #1 )

 
yeah, that too.
 
Math_France
Posts:23
Registered: 11/7/02
Re: Useful Code of the Day: Multipart Form File Upload   
Nov 20, 2003 2:17 AM (reply 3 of 19)  (In reply to original post )

 
Hi, excuse for my English.

I saw your code, and I think it can interest me. In fact, I would like to write in a file which is in my serveur, and I must use http protocole for do it. Do you think that your code can help me ?
To resume, I would like to do :

File fi = new File("http://myserver/myappli/hello.txt");
fi.write("Hello World");

I know that it's not simple that I write, but in your example you write in a file with a connection, and I suppose I can write that I wants in my file.

Thanks
 
gimbal2
Posts:5,223
Registered: 9/10/03
Re: Useful Code of the Day: Multipart Form File Upload   
Nov 20, 2003 2:44 AM (reply 4 of 19)  (In reply to #3 )

 
you mean append to the file right? That would involve first downloading it, making the changes and then uploading it again.
 
tomvollerthun
Posts:407
Registered: 11/11/03
Re: Useful Code of the Day: Multipart Form File Upload   
Nov 20, 2003 2:49 AM (reply 5 of 19)  (In reply to #4 )

 
As you mention useful code, I'd like to remind you of bbritta's wonderful munger...
 
tomvollerthun
Posts:407
Registered: 11/11/03
Re: Useful Code of the Day: Multipart Form File Upload   
Nov 20, 2003 2:52 AM (reply 6 of 19)  (In reply to #5 )

 
8HJF5PpFFSJK5PMM0AcKX1bP04VGPSxN0GpG0U4JSPqX3GepFtiD53pG50x3UmGaAjvm3a
 
bsampieri
Posts:14,508
Registered: 02/04/99
Re: Useful Code of the Day: Multipart Form File Upload   
Nov 21, 2003 10:18 AM (reply 7 of 19)  (In reply to #3 )

 
Math...

Yes, it is specifically for file upload via HTTP. This is only the client part, what the web browser would do with a file upload form. The server-side for the file upload stuff you have to do yourself, but there are many existing libraries for that. JspSmartUpload and built-in support in Struts, to name 2.

tomvollerthun: What is that? I don't know about "bbritta's wonderful munger". Maybe you send me that and I'll xlate it...
 
rednael
Posts:24
Registered: 12/10/00
Re: Thanks :-)   
Dec 7, 2003 6:30 AM (reply 8 of 19)  (In reply to original post )

 
Thanks dude, this is exactly what I've been looking for :-)
 
The-Sue
Posts:2,275
Registered: 10/18/03
Re: Useful Code of the Day: Multipart Form File Upload   
Dec 7, 2003 6:54 AM (reply 9 of 19)  (In reply to #6 )

 
8HJF5PpFFSJK5PMM0AcKX1bP04VGPSxN0GpG0U4JSPqX3GepFtiD53p
50x3UmGaAjvm3a

Unfortunately I can't find the post anymore.
 
hamiltonjose
Posts:1
Registered: 2/1/04
Re: Useful Code of the Day: Multipart Form File Upload   
Feb 1, 2004 6:41 AM (reply 10 of 19)  (In reply to original post )

 
Hi folks,

Actually, this code does not crash, but it also does not works... (with me, of course)
Everything appears to work, but the file is not uploaded.
I'm using W2K, IIS (local) and my url is <<http://localhost/upload/uploadarquivo.asp>>
If someone have any ideas, I'd appreciate a lot!
Thank you!

Hamilton.
 
schubem_01
Posts:1
Registered: 2/2/04
Re: Useful Code of the Day: Multipart Form File Upload   
Feb 2, 2004 1:43 PM (reply 11 of 19)  (In reply to #10 )

 
The Code will work perfectly if you change

out.writeBytes("Content-Disposition: form-data; name=\"" name "\" filename=\"" fileName "\"");

into

out.writeBytes("Content-Disposition: form-data; name=\"" name "\"; filename=\"" fileName "\"");
 
bbritta
Posts:8,810
Registered: 10/4/00
Re: Useful Code of the Day: Multipart Form File Upload   
Feb 2, 2004 2:50 PM (reply 12 of 19)  (In reply to #9 )

 
8HJF5PpFFSJK5PMM0AcKX1bP04VGPSxN0GpG0U4JSPqX3GepFtiD53p

50x3UmGaAjvm3a

Unfortunately I can't find the post anymore.

Wow, it's like seeing my name in lights....
http://forum.java.sun.com/thread.jsp?forum=54&thread=406096
 
mschipperheyn
Posts:8
Registered: 8/13/01
Re: Useful Code of the Day: Multipart Form File Upload   
Mar 7, 2004 9:41 AM (reply 13 of 19)  (In reply to original post )

 
I have a weird problem here. I'm using the Java POST (slightly altered for my purposes) mechanism but the receiving script doesn't receive any form variables (neither files nor regular form fields). If I try using the same receiving script with a regular upload form, it works. Because I've been pulling my hair out, I even went so far as to analyze the ip-packets. I can see that the file is actually sent through the Java POST mecahnism. Anybody any ideas. Here's the ip-packet of the failing upload:


Hypertext Transfer Protocol
HTTP: POST /fotoxs/upload.cfm HTTP/1.1
HTTP: Connection: Keep-Alive
HTTP: Content-Type: multipart/form-data; boundary=-----------------------------fb2649be18
HTTP: User-Agent: Mozilla/4.7 [en] (WinNT; U)
HTTP: Accept-Language: en-us
HTTP: Accept-Encoding: gzip, deflate
HTTP: Accept: image/gif, image/jpeg, image/pjpeg, /
HTTP: CACHE-CONTROL: no-cache
HTTP: Host: newmarc
HTTP: Content-Length: 15511
*********************************************************************

Hypertext Transfer Protocol
HTTP: -----------------------------fb2649be18
HTTP: Content-Disposition: form-data; name="uploadFile"; filename="out.jpg"
HTTP: Content-Type: image/jpeg
HTTP: Data
....[data packets]

The one that works (from the regular post) is as follows:


Hypertext Transfer Protocol
HTTP: POST /fotoxs/upload.cfm HTTP/1.1
HTTP: Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-gsarcade-launch, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, /
HTTP: Referer: http://localhost:8500/fotoxs/test.cfm
HTTP: Accept-Language: nl
HTTP: Content-Type: multipart/form-data; boundary=---------------------------7d41961f201c0
HTTP: Accept-Encoding: gzip, deflate
HTTP: User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
HTTP: Host: newmarc
HTTP: Content-Length: 59022
HTTP: Connection: Keep-Alive
HTTP: Cache-Control: no-cache

****************************************************************************

Hypertext Transfer Protocol
HTTP: -----------------------------7d41961f201c0
HTTP: Content-Disposition: form-data; name="FiletoUpload"; filename="D:\My Documents\My Pictures\fotomarc.jpg"
HTTP: Content-Type: image/pjpeg
HTTP: Data

***********************************************************************

Hypertext Transfer Protocol
HTTP: HTTP/1.1 100 Continue
HTTP: Server: Microsoft-IIS/5.0
HTTP: Date: Sun, 07 Mar 2004 17:14:41 GMT
....[data packets]

One detail worth mentioning is that I don't read a file from harddisk, I'm streaming it directly to the connection outputstream from an image compression utility. But I can see the data in the ip-packets, and even if that somehow didn't work right, it doesn't explaing the fact that the receiving scripts reports no form fields whatsoever.


Thanks,

Marc
 
bsampieri
Posts:14,508
Registered: 02/04/99
Re: Useful Code of the Day: Multipart Form File Upload   
Mar 8, 2004 9:48 AM (reply 14 of 19)  (In reply to #13 )

 
what does "slightly altered" mean?

Are you sure the script on the server supports multipart/form-data?
 
This topic has 19 replies on 2 pages.    1 | 2 | Next »
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 133

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums