Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Html Code:
XML
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>

</head>
<body>
<form Action="index" method="get" enctype="multipart/form-data">
<input type="text" name=text1>
<input type="file" name=img>
<input type="submit" name="submit">
</form>
</body>
</html>



JSP CODE:

@WebServlet("/index")
public class index extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* Default constructor.
*/
public index() {
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String source=request.getParameter("img");
System.out.println("Path is "+source);
File S=new File("E:\\k"+source);
File D=new File("E:\\photos");

System.out.println("hello");
try
{
if(S.exists())
{
if(!D.exists())
{
D.createNewFile();
System.out.println("new file created");
}
if(D.exists())
{
FileChannel SS=null;
FileChannel DD=null;
SS=new FileInputStream(S).getChannel();
DD=new FileOutputStream(D).getChannel();
if(SS!=null && DD!=null)
{
DD.transferFrom(SS, 0, SS.size());
System.out.println("Successfully copied");

}
if(SS!=null)
{
SS.close();
}
if(DD!=null)
{
DD.close();
}

}
}
else
{
System.out.println("No file Exist with that name");
}
}
catch(NullPointerException ne)
{
System.out.println("Error"+ne);
}
catch(Exception e)
{
System.out.println("General Error"+e);
}







}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}
Posted

1 solution

I do not have time to go through the back end code, but I noticed one mistake in the html page, that is the form method must be 'post' not 'get' for file upload operation. And you should enclose the value of input attributes with quotes. Hope that solves your problem.
Refer this for further help: http://www.tutorialspoint.com/jsp/jsp_file_uploading.htm[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900