aspose file tools*
The moose likes Servlets and the fly likes Urgent ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Urgent !" Watch "Urgent !" New topic
Author

Urgent !

Jimi Rock
Ranch Hand

Joined: Feb 19, 2001
Posts: 134
Hi all ,
I would be grateful if some one could help me fixing the error that I am facing .
I am trying to use servlet to upload a file of any format from the client side to the server.
The servlet code is as follows:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class UploadServlet extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponseres)
throws ServletException, IOException {
String file = req.getPathTranslated();
InputStream in = req.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
byte[] buf=new byte[4*1024];
int bytesRead;
try {
while((bytesRead=in.read(buf)) != -1) {
fos.write(buf, 0, bytesRead);
}
} finally {
if(fos!=null) fos.close();
}
}
}

And the following is the client side code:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
public class ClientUpload {

public void write(String sourcefile, String destFile, String servletURL)
throws IOException {
URL url = new URL(servletURL+ "/" + destFile);
URLConnection con = url.openConnection();
OutputStream out = con.getOutputStream();
FileInputStream fis = new FileInputStream(sourcefile);
byte[] buf=new byte[4*1024];
int bytesRead;
try {
con.setDoOutput(true);
con.connect();
while((bytesRead=fis.read(buf)) !=-1) {
out.write(buf,0,bytesRead);
}
}
finally {
if(fis!=null) fis.close();
}
}
}
class ClientUploadMain{
public static void main(java.lang.String[] args) {
ClientUpload client = new ClientUpload();
try{
client.write("D:/CatalogServlet.java","C:/CatalogServlet.java","http://localhost:8080/examples/servlet/UploadServlet");
}catch (Exception ex) {
System.out.println("File Not Found");}
}
}

Please try it and tell me why File Not Found?
Thanks,
Jimi.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Jimi,
Are you testing this code in windows platform? If so, the source and destination files should have 2 backward slashes \\ instead of a single forward slash.
Try to test like this.
D:\\CatalogServlet.java OR C:\\CatalogServlet.java
regds
maha anna
Jimi Rock
Ranch Hand

Joined: Feb 19, 2001
Posts: 134
I have tried what you suggested.
I am facing now the following error :
cannot write to URLConnection if doOutput = false -
call setDoOutput(true)
Please try the code and see what you get , and tell me please what can I do to fix the error .This is an urgent problem!
Thanks in advance.
Jimi.
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
Try shifting the con.setDoOutput(true ) and con.connect() statement so that they come BEFORE the con.getOutputStream(). Put them immediately after openConnection().

[This message has been edited by Rahul Rathore (edited April 03, 2001).]
Jimi Rock
Ranch Hand

Joined: Feb 19, 2001
Posts: 134
The following run-time error has been generated:
"Cannot reset method . Already connected!"
I have tried to remove con.connect() , no run time error ,but the file is not uploaded from D: to C:
Please Help!!!
Thanks for all,
Jimi.
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
Instead of req.getPathTranslated(), do
String file=req.getPathInfo();
To ensure that the target file name is correctly extracted, include a System.out.println(file) statement immediately after the above statement. If the problem persists tell us the name of the file which is printed to the system console.
Jimi Rock
Ranch Hand

Joined: Feb 19, 2001
Posts: 134
No thing printed!!!
note that I have removed "con.connect()" because of the error I have specified earlier.
I have compiled the client and the server programs and then run the ClientUploadMain , but no thing printed.
Please help!
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
Try this- in the write() method after the statement
if(fis!=null) fis.close();
Add this statement also:
if(out!=null) out.close();
Also in the first line of the doPost() method include a statement:
System.out.println("This is invoked servlet")
Tell if this line gets printed.

[This message has been edited by Rahul Rathore (edited April 04, 2001).]
Jimi Rock
Ranch Hand

Joined: Feb 19, 2001
Posts: 134
Nothing Printed again! so the servlet is not invoked...
I wanna cry!!!
Please tell me about the following:
-Is it necessary to run Apache with tomcat?
-What should be included (exactly) in the parameters of the "client" object "write" method.(which I have declared as an instance of "ClientUpload" class)?
-What if I don't want to use the ClientUpload class? what if I want to use html noly instead(adding doGet method...)
note that I don't know how to implement it , so I need the code if you agree.
Thank you very much,
Jimi.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Jimi,
Wrote both Client side java application and SeverSide servlet which can be used to upload a file from client to server.
You can use this code.
Beleive me, spent whole 3 hours. Haven't used this java.net package before.
Wanted to help you. I also got the same errors which you got in previous posts. But didn't want to fail.
Searched google,javasoft's forums etc and finally got it working!
Have given comments in between. You can cut and paste this code and give it a try!
I almost forgot to say! No need for Apache. Just Tomcat alone enough.
With best wishes,
Maha Anna



[This message has been edited by maha anna (edited April 04, 2001).]
Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
Hi Jimi, I was just passing by this forum, I really do not know much about servlets, so I cannot tell you if this is going to work or, the best you can do is watch for your self.
There this Servlet File upload in http://java.isavvix.com
in the "code exchange" section, you'll find it easily because is the top download.

I hope this helps
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
I am an old member of the Maha fan club. I am sure that Jimi would like to join the club.
Jimi is it OK now? Or do you still have problems?
VidyaSri
Greenhorn

Joined: Apr 05, 2001
Posts: 6
Hi rahul,
I am very new to this forum.Can u send me some details about this forum and also about the Maha fan club. I would like to be an active participant for the same since I am preparing for the java cerfication I.
Jimi Rock
Ranch Hand

Joined: Feb 19, 2001
Posts: 134
Hi all,
it is ...............................working!!!
Thanks Maha Anna for giving me your precious time , Of course I would like to be a member in your fan club
Thanks Rahul because you have followed my problem step by step till it has been solved
Thanks again and again for all...
Best Regards,
Jimi.
Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
Just a quick question to help me understand your code, why do you create a new BufferedReader at the end of the UploadFile class and return that? What does that do?
Thanks
Brian
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Brain,
I asked the same question over and over again myself. When I tried the method with code which only write data to urlConnection object, the servlet is not even get called.
I really don't know why? Initially I wrote this method with 'void' return type , without the last line urlc.getInputStream(). But the servlet was not even invoked.
Did lots of research. Even in Sun's network tutorial, (see the last paragraph!), they quietly say, " when we write something to server side, it is MOST LIKELY we expect to receive the response and process them" like that.
Is it so? Or we MUST get the urc.getInputStream() whenever we write something to server, in order to make the code work? This is the question still hangs in mind unanswered.
We need not send back BuffereedReader. Just calling urlC.getInputStream() alone enough to make the code work.
regds
maha anna
http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html


[This message has been edited by maha anna (edited April 05, 2001).]
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
Oops
I have been receiving e-mail seeking info about the Maha fan club, and expressing a desire to join it.
Sorry guys- I was just admiring maha anna. I was just saying that I am an old fan of hers -Because I too have learnt a lot from her posts.
There is no formal 'Maha fan club' in existence- at least not to my knowledge. But there are countless people out there indebted for her help and guidance.
Sorry again for the confusion.
Jimi Rock
Ranch Hand

Joined: Feb 19, 2001
Posts: 134
Hi Rahul,
many people now have fixed this confusion.
Thanks for your reply,
Jimi.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
I was also wondering 'Where is Maha's fan club?' and thought Rahul is saying in general and left off. There is no official fan club. People generally contact through email.
regds
maha anna
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Just to throw in some additional info: you can do this with nothing more than an HTML form using multi-part on the client side. No applet is needed. Information about this including sample classes can be found in the O'Reilly book, Java Servlets.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Thanks Thomas. I have used that upload class. May be some time latter...
regds
maha anna
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
Originally posted by Thomas Paul:
Just to throw in some additional info: you can do this with nothing more than an HTML form using multi-part on the client side. No applet is needed. Information about this including sample classes can be found in the O'Reilly book, Java Servlets.

The above client code cannot be used with an applet unless we go through the horrible rigmarole of signing applets and giving permissions. Its only feasable to use with frame.
I have the first edition of O'Reilly which warns about file uploading through HTML

...it's important to note that file uploading is experimental and not supported in all browsers.

I wonder whether it has now passed that 'experimental' stage.
Of course using a comprehensive and standard protocol (RFC 1867) makes HTML file uploading more attractive. However servlet programming is more complicated. Fortunately we have free classes like com.oreilly.servlet.MultipartRequest to do the work.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Rahul,
I downloaded the lattest version of MultiPartRequest ans tested. That's the class I meant. Don't have hunter's book though. But have Marty Hall's.
regds
maha anna
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
Continuing discussion on Paul's suggestion.
There are situations in which we want to use applet or frame to upload (rather than an HTML page), such as where uploading is one of several facilities of a full featured application. In such cases we want to use URL/URLConnection since it is more convenient, then writing directly to a socket etc.
So if I want to use the HTML uploading method in a frame/applet, then what would be the client program?
On a bare first perusal of RFC 1867, it appears that programming the client using URL/URLConnection will be horribly complicated. I am trying to work it out. I wonder if somebody has already used this protocol in frame/applet.
Also RFC 1867 is an experimental protocol, liable to change. So the advantage of using a standard protocol is also not there. This is what the RFC says:-

This memo defines an Experimental Protocol for the Internet community. This memo does not specify an Internet standard of any kind. Discussion and suggestions for improvement are requested.


[This message has been edited by Rahul Rathore (edited April 08, 2001).]
Jimi Rock
Ranch Hand

Joined: Feb 19, 2001
Posts: 134
I am planning now to develop the uploading process in order to have a user interface . I think that I have two options :
* The user Interface is going to be a normal HTML page.
* Develop the client program to be a java frame.
I prefer the second option as I see the situation through your replies.
I thought that no problem with the frame , and it is easy to be implemented.
Please provide me with your comments!
Thanks,
Jimi.
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
As per my limited knowledge this appears to be the best option currently:-
1. Use a frame.
2. In the frame use the simple code given by maha anna (instead of using RFC 1867).
However RFC 1867 is designed to make binary uploading through POST more efficient. I am not sure how it does so.

Originally posted by maha anna


Is it so? Or we MUST get the urc.getInputStream() whenever we write something to server, in order to make the code work? This is the question still hangs in mind unanswered.


I found this beautiful Javaworld article answering maha anna's question:- http://www.javaworld.com/javaworld/jw-03-2001/jw-0323-traps.html
The author says that it is con.getInputStream() which actually posts the request to the server !!
1. When we do con.getOutputStream() and write to the output stream, actually that writing is done to a local buffer- NOT to the server.
2. When we do con.getInputStream()- it is now that what is in the local buffer is posted to the server !!
The author warns of several traps for eg:-
TRAP 1:
--you do con.getInputStream()
--you do con.getOutputStream()
--you write to output stream
Result=Failure: When getInputStream is done nothing has been written to the POST buffer- so nothing is posted - and subsequent opening of output stream throws exception.
TRAP 2:
--you do con.getOutputStream()
--you write to output stream and flush
--you do NOT do con.getInputStream()
Result=Failure: The writing to the output stream merely writes to local buffer. Without getInputStream() nothing is posted to the server.
TRAP 3:
--you do con.getOutputStream()
--you do con.getInputStream() //before writing to output stream
--you write to output stream and flush
Result=Failure: At the time getInputStream is done, nothing has been written to the the local POST buffer. So nothing is posted to the server even though the server/servlet is invoked. Subsequent writing is useless.
SUCCESSFUL METHOD:
--you do con.getOutputStream()
--you write to output stream and flush
--you do con.getInputStream()
Result=Success. This is the order which we must strictly and compulsorily adhere to.
I recall many other posts where similar problems was faced, that either servlet was not invoked, or even if invoked nothing was posted. This javaworld should answer a lot of questions.

[This message has been edited by Rahul Rathore (edited April 13, 2001).]
Jimi Rock
Ranch Hand

Joined: Feb 19, 2001
Posts: 134
Hi all,
I have implemented the client program to have a user interface for file uploading process.the servlet works fine on a local host and also over LAN.
regarding our current servlet limitation related to the memory stack segment limited size, and as Rahul has suggested, I agree that we need to improve the performance of that servlet to handle the uploading process of large video files (up to 100MB) ,this could be done by dividing the uploading process into multiple parts in order to upload the large file as multiple segments individually.
I will be grateful for any one that has a suggested code, I will be happy also to test its performance.
Best Regards,
Jimi.
Jimi Rock
Ranch Hand

Joined: Feb 19, 2001
Posts: 134
Hi all,
Especially for Rahul, Maha ,
Any comments will be appreciated.
Thanks.
vaibhav punekar
Ranch Hand

Joined: Jan 20, 2001
Posts: 134
Hi,
Jimi,
I m currently working on the file uploading part.I ve successfully used the "com" package downloaded from Oreilly's site.The Oreilly servlet book had that code previously but it was problematic.Now with this "bunch" of classes there is no problem.You can upload any file of any size.


VAIBHAV <BR>SCJP
Jimi Rock
Ranch Hand

Joined: Feb 19, 2001
Posts: 134
Thanks Vaibhav,
Can you provide me with info about the site , and from where can I get the code.
Thanks.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

The URL is as follows:
http://www.oreilly.com/catalog/jservlet or http://www.servlets.com
You can find a link to the com.oreilly.servlet
pkg in the left navigation bar.
regds.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Urgent !