Html Code:
<!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
}
}