Upload image in folder.
package cd;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javazoom.upload.MultipartFormDataRequest;
import javazoom.upload.UploadBean;
import javazoom.upload.UploadException;
import javazoom.upload.UploadFile;
/**
*
* @author naveen
*/
public class my extends HttpServlet {
private String Fullpath1=null;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, UploadException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
Hashtable ht=null;
UploadFile file=null;
try
{
MultipartFormDataRequest mfdr=new MultipartFormDataRequest(request);
UploadBean upb=new UploadBean();
upb.setFolderstore("~/images/");
upb.setOverwrite(false);
ht=mfdr.getFiles();
Enumeration files=ht.elements();
file=(UploadFile)files.nextElement();
String name=file.getFileName();
out.print(name);
String FullPath="~/images/"+name;
Fullpath1=FullPath;
upb.store(mfdr);
out.println("<html>");
out.println("<body>");
out.println("<img src="+Fullpath1 +" height=100% width=100% >");
out.println("</body>");
out.println("</html>");
out.println(Fullpath1);
Bean bean=new Bean(Fullpath1);
bean.save();
}
catch(UploadException e)
{
e.printStackTrace();
System.out.print(e);
}
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (UploadException ex) {
Logger.getLogger(my.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (UploadException ex) {
Logger.getLogger(my.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author naveen
*/
package cd;
import java.io.*;
import java.sql.*;
import java.util.*;
public class Bean
{
public Bean()
{
super();
}
String path;
public Bean(String path)
{
this.path=path;
}
public String getPath()
{
return path;
}
public void setPath(String path)
{
this.path=path;
}
public void save()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3305/file","root","root");
java.sql.PreparedStatement ps=con.prepareStatement("insert into load1(path) values(?)");
ps.setString(1,path);
ps.executeUpdate();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>my</servlet-name>
<servlet-class>cd.my</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>my</servlet-name>
<url-pattern>/my</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
<%--
Document : index
Created on : Jun 4, 2014, 12:09:21 PM
Author : naveen
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form enctype="multipart/form-data" action="my" method="Post" >
<table border='2'>
<tr>
<td></td>
<td><input type="file" name="image"></td>
</tr>
<tr>
<td colspan='2'><input type="submit" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>