Hello,
We are in the process of developing a J2EE application. In this, we require to upload a file from client end to server end. What are the posible ways to do this? Is Applet the only way?
Thanks and Regards,
Pankaj.
-
File Uploading (3 messages)
- Posted by: Pankaj Choudhary
- Posted on: November 09 2001 05:49 EST
Threaded Messages (3)
- File Uploading by Sheng Sheen on November 09 2001 16:33 EST
- File Uploading by Pankaj Choudhary on November 10 2001 07:02 EST
- File Uploading by Kari Sarsila on November 12 2001 03:57 EST
- File Uploading by Pankaj Choudhary on November 10 2001 07:02 EST
-
File Uploading[ Go to top ]
- Posted by: Sheng Sheen
- Posted on: November 09 2001 16:33 EST
- in response to Pankaj Choudhary
You can do it with a Servlet.
Here's some sample code.
public void doPost(HttpServletRequest req, HttpServletResponse
res) {
PrintWriter out = null;
DataInputStream in = null;
FileOutputStream fileOut = null;
rootPath = "c:\\upload
";
try {
res.setContentType("text/html");
out = res.getWriter();
String contentType = req.getContentType();
java.util.Enumeration enum =
req.getParameterNames();
while(enum.hasMoreElements()) {
out.println(" param = " +
(String)enum.nextElement());
}
out.println(contentType);
if ((contentType != null)
&& (contentType.indexOf("multipart/form-
data") >= 0)) {
in = new DataInputStream
(req.getInputStream());
int formDataLength = req.getContentLength
();
out.println("1<br>");
if (formDataLength > MAX_SIZE) {
out.println("Sorry file is too
large ");
out.flush();
return;
}
out.println("2<br>");
byte dataBytes[] = new byte
[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes,
totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
out.println("3<br>");
String file = new String(dataBytes);
String saveFile = file.substring
(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0,
saveFile.indexOf("\n"));
saveFile =
saveFile.substring
(saveFile.lastIndexOf("
") + 1, saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf
("=");
String boundary = contentType.substring
(lastIndex + 1, contentType.length());
The rest of the code got cutoff because TSS had a message length restriction. -
File Uploading[ Go to top ]
- Posted by: Pankaj Choudhary
- Posted on: November 10 2001 07:02 EST
- in response to Sheng Sheen
Hello Sheng,
I couldn't get complete code. Can you please send it back to pankaj08 at sify dot com. I will be thankfull to you.
Thanks and Regards,
Pankaj.
-
File Uploading[ Go to top ]
- Posted by: Kari Sarsila
- Posted on: November 12 2001 03:57 EST
- in response to Pankaj Choudhary
the propably best way to do this is using jason hunters MultipartRequest, see www.servlets.com for details and code downloads.
Regards,
Kari