permaculture playing cards*
The moose likes Servlets and the fly likes Client-to-Server file upload using servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Client-to-Server file upload using servlet" Watch "Client-to-Server file upload using servlet" New topic
Author

Client-to-Server file upload using servlet

Sandeep Parmar
Greenhorn

Joined: Aug 06, 2008
Posts: 10
Hi Experts,
Can anyone tell me will the following code work for client-to-server file upload? Its working fine in a environment where client and server are on same machine. Somebody told me that FileInputStream does not work for client-server env. What is the solution...anyone? Thank you.

public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
String srcDir = "/Users/Downloads";
String destDir = "/Users/workspace/server/devel/soa/web/WEB-INF";
String filName = "photo.jpg";
String newName = "xyz.jpg";

try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcDir + File.separator + filName));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destDir + File.separator + newName));
byte[] buf = new byte[1024];
int len;
while ((len = bis.read(buf)) > 0) {
bos.write(buf, 0, len);
}
bos.close();
bis.close();
} catch(IOException ioe) {
logger.error("IOEeception in UploadServlet = " + ioe.getMessage());
}
}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the .


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

No. it will not work.

For file uploading you need to send the file as part of a multi-part request. See the JSP FAQ entry on this subject for more information.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Client-to-Server file upload using servlet