Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

reading file path from JSP in servlet

807589Oct 17 2008 — edited Oct 17 2008
Hi Experts,I am using a JSP code to upload a file through servlet. My problem is I can not read the filepath in servlet. Can anyone tell me what is wrong? request.getParameter() doesnt work.
Here is my JSP and Servlet code

<table>
<tr>
<form method="post" enctype="multipart/form-data" action="UploadServlet">
<td> <input type="file" size=20 name="fname"> </td>
<td> <input type="Submit" value="Upload"> </td> </form>
</tr>
</table>


and servlet :

public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
String filepath = httpservletrequest.getParameter("fname");
logger.error("@@@@filepath = " + filepath); // prints null
String srcDir = "/Users//Downloads";// hard coded
String destDir = "/Users//workspace/server/devel/soa/web/WEB-INF"; // hard coded
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());
}


}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 14 2008
Added on Oct 17 2008
4 comments
1,865 views