Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Client-to-Server file upload using servlet

 
Sandeep Parmar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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());
}
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic