Thursday, May 26, 2011

File upload using spring framework

Upload file using spring framework


how to upload file using spring.

Upload a file using spring framework is easy.

first of all we must import the required spring classes(that are given below).

To upload file using spring framework we must import 'MultipartHttpServletRequest' and

'MultipartFile' in to our project.to deal with multi part content in a servelet request we must include

MultipartHttpServletRequest interface. MultipartFile interface is used to store file that are comming with

Multipart HttpServlet Request. Here when the user click the 'Upload Doc' button the corresponding handler function

works in this case 'upLoadHandler()' and the function mapp the request(@RequestMapping(value = "/fileupload.do", method =

RequestMethod.POST)).Then corresponding code executed.after uploading the file server redirect the request to upload page

again.




package fileUpload.document;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UploadController {


    ModelAndView mav;


//location where to upload file
    private static final String destinationDir = "/mnt/disk1/uploads/";


    @RequestMapping(value = "/upload.do", method = RequestMethod.GET)
    public String uploadPage()
    {
        return "document/upPage";
    }


    @RequestMapping(value = "/fileupload.do", method = RequestMethod.POST)
    public String upLoadHandler(HttpServletRequest req,
            HttpServletResponse res) throws Exception {


        res.setContentType("text/plain");
        if (!(req instanceof MultipartHttpServletRequest)) {
            res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Expected multipart request");
            return null;
        }
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;
        //here "fileUploaded" is the name of the html file upload tag in the web page
    MultipartFile file = multipartRequest.getFile("fileUploaded");
        File destination = new File(destinationDir + file.getOriginalFilename());
        file.transferTo(destination);       
        return "redirect:upload.do";
    }
}


file uploading html


this is the html page for uploading file in to server.only required part of the html

is given.

here we can see that the method is POST and encryption type is

multipart/form-data'

    <form method="post" action="/fileupload.do" enctype="multipart/form-data">
     <input type="file" name="fileUploaded" />
    <input type="submit" value="Upload Doc"/>
    </form>

before writing the code we must include multi part resolver bean in our application-context.xml file

<!-- Configure the multipart resolver -->
 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 <!-- one of the properties available; the maximum file size in bytes -->
 <property name="maxUploadSize" value="100000"/>
 </bean>


Thursday, May 19, 2011

how to make Entity classes using Hibernate mapping file and POJO from Database?

How to make entity classes using hibernate mapping file and POJO from Database.

Step by step way is given below.
using net beans you can done it easily

With NetBeans.

assuming that we created a new project named 'HBN_Test'.

then add hibernate and spring framework file to our project.


Making hibernate configuration wizard file

then we need to configure hibernate configuration file in to our program


1.Right click the project then in the 'new' option select
hibernate configuration wizard option.it will open a new window.
2.Then click next and select appropriate database connection and
and we can see that database dialect is
'org.hibernate.dialect.MySQLDialect'
3.click finish button
this fill connect hibernate with our database
then


Making hibernate reverse engineering wizard file


1.Right click the project then in the 'new' option select
hibernate reverse engineering wizard option.it will open a new window
2.click next then we can see the tables in our data base select wanted tables check if we want to include related tables(forigen key referance tables)
3.click finish button.

this fill connect hibernate with our selected tables
then


Making hibernate mapping file and pojo from database



1.Right click the project then in the 'new' option select
hibernate mapping file and pojo from database option.it will open a new window
2.select our hibernate configuration file(hibernate.cfg.xml) and also hibernate reverse engineering wizard(hibernate.reveng.xml)
3.In general setting section select jdk 5 language features and EJB 3
annotation if we want.
4.then select appropriate package to import our entity classes.
5.then click finish button.
6.this will makes entity classes for our tables in the database.