google Analytics

Friday, July 23, 2010

Spring File Upload Download full Example with Oracle Database

create table FILES
(
ID NUMBER(10) not null,
FILENAME VARCHAR2(100) not null,
NOTES VARCHAR2(100),
TYPE VARCHAR2(40),
FILES BLOB
);
public class Files {
private int id;
private String filename;
private String notes;
private String type;
private byte[] file;
public int getId() {
return id;
}
.......
}
public class FilesService extends JdbcDaoSupport {
String query = null;
public Files find(int id) {
query = "select * from files where id = ?";
try {
Files file = (Files) getJdbcTemplate().queryForObject(query, new Object[] {id},
new RowMapper() {
Files fl;
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
fl = new Files();
fl.setId(rs.getInt(1));
fl.setFilename(rs.getString(2));
fl.setNotes(rs.getString(3));
fl.setType(rs.getString(4));
fl.setFile(rs.getBytes(5));
return fl;
}
});

return file;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public List listAll() {
query = "select id, filename, notes, type from files";

try{
List files = getJdbcTemplate().query(query, new BeanPropertyRowMapper(Files.class));\
return files;
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
public void save(final Files file) {
query = "insert into files (filename, notes, type, file) values (?, ?, ?, ?)";
try {
synchronized(this) {
getJdbcTemplate().update(new PreparedStatementCreator() {

public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement statement = con.prepareStatement(query);
statement.setString(1, file.getFilename());
statement.setString(2, file.getNotes());
statement.setString(3, file.getType());
statement.setBytes(4, file.getFile());
return statement;
}
});
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void delete(int id) {
query = "delete from files where id = ?";

try {
getJdbcTemplate().update(query, new Object[] {id});
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public class FilesForm extends AbstractController {
private FilesService filesService;

public void setFilesService(FilesService filesService) {
this.filesService = filesService;
}

protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {

List files = this.filesService.listAll();

return new ModelAndView("files", "files", files);
}
}
public class FilesController extends MultiActionController {
private FilesService filesService;

public void setFilesService(FilesService filesService) {
this.filesService = filesService;
}
public ModelAndView upload(HttpServletRequest request,
HttpServletResponse response) throws Exception {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile multipartFile = multipartRequest.getFile("file");
Files file = new Files();
file.setFilename(multipartFile.getOriginalFilename());
file.setNotes(ServletRequestUtils.getStringParameter(request, "notes"));
file.setType(multipartFile.getContentType());
file.setFile(multipartFile.getBytes());
this.filesService.save(file);
return new ModelAndView("redirect:files.htm");
}
public ModelAndView download(HttpServletRequest request,
HttpServletResponse response) throws Exception {
int id = ServletRequestUtils.getRequiredIntParameter(request, "id");
Files file = this.filesService.find(id);
response.setContentType(file.getType());
response.setContentLength(file.getFile().length);
response.setHeader("Content-Disposition","attachment; filename=\"" + file.getFilename() +"\"");
FileCopyUtils.copy(file.getFile(), response.getOutputStream());
return null;
}
public ModelAndView delete(HttpServletRequest request,
HttpServletResponse response) throws Exception {
int id = ServletRequestUtils.getRequiredIntParameter(request, "id");
this.filesService.delete(id);
return new ModelAndView("redirect:files.htm");
}

}



(&?xml version="1.0" encoding="UTF-8"?(&
(&beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"(&

(&!-- datasource --(&
(&bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"(&
(&property name="driverClassName" value="com.mysql.jdbc.Driver" /(&
(&property name="username" value="root"(&(&/property(&
(&property name="password" value="root"(&(&/property(&
(&property name="url" value="jdbc:mysql://localhost:3306/test"(&
(&/property(&
(&/bean(&
(&!-- template --(&
(&bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"(&
(&property name="dataSource" ref="dataSource" /(&
(&/bean(&
(&!-- services --(&
(&bean id="filesService" class="com.spring.example.service.FilesService"(&
(&property name="jdbcTemplate" ref="jdbcTemplate"/(&
(&/bean(&
(&/beans(&

(&?xml version="1.0" encoding="UTF-8"?(&
(&beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"(&

(&!-- mapping --(&
(&bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"(&
(&property name="mappings"(&
(&props(&
(∝ key="files.htm"(&filesForm(&/prop(&
(∝ key="upload.htm"(&filesController(&/prop(&
(∝ key="download.htm"(&filesController(&/prop(&
(∝ key="delete.htm"(&filesController(&/prop(&
(&/props(&
(&/property(&
(&/bean(&

(&!-- The view resolver --(&
(&bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" /(&

(&bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"(&
(&property name="maxUploadSize" value="5242880" /(&
(&/bean(&

(&!-- controller --(&
(&bean id="filesForm" class="com.spring.example.web.FilesForm"(&
(&property name="filesService" ref="filesService"/(&
(&/bean(&

(&bean id="filesController" class="com.spring.example.web.FilesController"(&
(&property name="filesService" ref="filesService"/(&
(&property name="methodNameResolver"(&
(&bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"(&
(&property name="mappings"(&
(&props(&
(∝ key="/upload.htm"(&upload(&/prop(&
(∝ key="/download.htm"(&download(&/prop(&
(∝ key="/delete.htm"(&delete(&/prop(&
(&/props(&
(&/property(&
(&/bean(&
(&/property(&
(&/bean(&
(&/beans(&

2 comments:

  1. <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

        <!-- mapping -->
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="files.htm">filesForm</prop>
                    <prop key="upload.htm">filesController</prop>
                    <prop key="download.htm">filesController</prop>
                    <prop key="delete.htm">filesController</prop>
                </props>
            </property>
        </bean>

        <!-- The view resolver -->
        <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              p:prefix="/WEB-INF/jsp/"
              p:suffix=".jsp" />

        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="maxUploadSize" value="5242880" />
        </bean>

        <!-- controller -->
        <bean id="filesForm" class="com.spring.example.web.FilesForm">
            <property name="filesService" ref="filesService"/>
        </bean>

        <bean id="filesController" class="com.spring.example.web.FilesController">
            <property name="filesService" ref="filesService"/>
            <property name="methodNameResolver">
                <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
                    <property name="mappings">
                        <props>
                            <prop key="/upload.htm">upload</prop>
                            <prop key="/download.htm">download</prop>
                            <prop key="/delete.htm">delete</prop>
                        </props>
                    </property>
                </bean>
            </property>
        </bean>
    </beans>

    ReplyDelete
  2. <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

        <!-- datasource -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="username" value="root"></property>
            <property name="password" value="root"></property>
            <property name="url" value="jdbc:mysql://localhost:3306/test">
            </property>
        </bean>

        <!-- template -->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource" />
        </bean>

        <!-- services -->
        <bean id="filesService" class="com.spring.example.service.FilesService">
            <property name="jdbcTemplate" ref="jdbcTemplate"/>
        </bean>
       
    </beans>

    ReplyDelete