On Nov 19, 2009, at 4:49 AM, Paul Sandoz wrote:
>
>
> We do not have a good file upload example available yet, but you can find one here:
>
> http://markmail.org/message/quaoa3s73544vxfp?q=list:net.java.dev.jersey.users+fileupload+order:date-backward&page=1
Here is another example. I needed to svn check out
https://mimepull.dev.java.net/ and build the jar.
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;
@Path("/uploader")
public class Uploader {
protected int TOTAL_KB = 100;
private FileUpload doUpload(String path, InputStream is) {
FileUpload fileUpload = new FileUpload();
fileUpload.isTooBig = false;
OutputStream out = null;
File f = new File(path);
fileUpload.file = f;
try {
out = new FileOutputStream(f);
byte buf[] = new byte[1024];
int len, count = 0;
while ((len = is.read(buf)) > 0) {
if ((count++) == TOTAL_KB) {
fileUpload.isTooBig = true;
}
out.write(buf, 0, len);
}
System.out.println("\nFile is created...................................");
fileUpload.size = count * 1024;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return fileUpload;
}
//_at_Consumes(MediaType.MULTIPART_FORM_DATA_TYPE)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@POST
@Produces("text/plain")
public String upload(@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition contDisp,
@FormDataParam("title") String title,
@FormDataParam("description") String description) {
System.out.println("fileInputStream: " + fileInputStream);
FileUpload fileUpload = doUpload(contDisp.getFileName(), fileInputStream);
File theFile = fileUpload.file;
System.out.println("theFile: " + theFile);
if (fileUpload.isTooBig) {
theFile.delete();
System.out.println("File was too big and was deleted. Returning '-1'");
return "-1";
}
int fileSize = fileUpload.size;
System.out.println("fileSize: " + fileSize);
System.out.println("contDisp: " + contDisp);
System.out.println("contDisp getFileName: " + contDisp.getFileName());
System.out.println("contDisp getName: " + contDisp.getName());
System.out.println("contDisp getSize: " + contDisp.getSize());
System.out.println("contDisp getType: " + contDisp.getType());
System.out.println("contDisp getCreationDate: " + contDisp.getCreationDate());
System.out.println("contDisp getModificationDate: " + contDisp.getModificationDate());
System.out.println("contDisp getReadDate: " + contDisp.getReadDate());
Map<String, String> params = contDisp.getParameters();
System.out.println(" ---- params ----");
for (String key : params.keySet()) {
System.out.println("contDisp " + key + "=" + params.get(key));
}
System.out.println("title: " + title);
System.out.println("description: " + description);
return "1";
}
public class FileUpload {
int size;
boolean isTooBig;
File file;
}
}