• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Client-to-Server file upload using servlet

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}
}
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic