Announcement Announcement Module
Collapse
No announcement yet.
File Uploading problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • File Uploading problem

    Guys,
    I'm trying to write a simple spring project thats work is to upload a file. But somehow Im getting some exception, but I cant get it what is actually going on here........

    here is my Controller class
    Code:
    package com.fileupload.web;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.util.FileCopyUtils;
    import org.springframework.web.bind.ServletRequestUtils;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.multipart.MultipartHttpServletRequest;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
    
    import com.fileupload.domain.Files;
    import com.fileupload.service.FilesService;
    
    public class FilesController extends MultiActionController {
    	private FilesService filesService;
    
    	public void setFilesService(FilesService filesService) {
    		this.filesService = filesService;
    	}
    
    	/**
    	 * upload
    	 */
    	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");
    	}
    
    	/**
    	 * download
    	 */
    	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;
    
    	}
    
    	/**
    	 * delete
    	 */
    	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");
    	}
    
    }
    Code:
    import java.util.List;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.AbstractController;
    
    import com.fileupload.domain.Files;
    import com.fileupload.service.FilesService;
    
    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> files = this.filesService.listAll();
    
    		return new ModelAndView("files", "files", files);
    	}
    }
    Service classs

    Code:
    package com.fileupload.service;
    
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.List;
    
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.PreparedStatementCreator;
    import org.springframework.jdbc.core.RowMapper;
    import org.springframework.jdbc.core.support.JdbcDaoSupport;
    
    import com.fileupload.domain.Files;
    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.PreparedStatement;
    
    public class FilesService extends JdbcDaoSupport {
    	String query = null;
    
    	/**
    	 * find
    	 */
    	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;
    	}
    
    	/**
    	 * listAll
    	 */
    	public List<Files> listAll() {
    		query = "select id, filename, notes, type from files";
    
    		try {
    			List<Files> files = getJdbcTemplate().query(query,
    					new BeanPropertyRowMapper(Files.class));
    
    			return files;
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    
    		return null;
    	}
    
    	/**
    	 * save
    	 */
    	public void save(final Files file) {
    		query = "insert into files (filename, notes, type, file) values (?, ?, ?, ?)";
    
    		try {
    			synchronized (this) {
    				getJdbcTemplate().update(new PreparedStatementCreator() {
    
    					@SuppressWarnings("unused")
    					public PreparedStatement createPreparedStatement(
    							Connection con) throws SQLException {
    						PreparedStatement statement = (PreparedStatement) 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;
    					}
    
    					@Override
    					public java.sql.PreparedStatement createPreparedStatement(
    							java.sql.Connection arg0) throws SQLException {
    						// TODO Auto-generated method stub
    						return null;
    					}
    				});
    			}
    		} catch (Exception ex) {
    			ex.printStackTrace();
    		}
    	}
    
    	/**
    	 * delete
    	 */
    	public void delete(int id) {
    		query = "delete from files where id = ?";
    
    		try {
    			getJdbcTemplate().update(query, new Object[] { id });
    		} catch (Exception ex) {
    			ex.printStackTrace();
    		}
    	}
    }

    Dispatcher servlet

    Code:
    <?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" />
    
    
    	<!-- Maximum file size 5 MB -->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="maxUploadSize" value="5242880" />
        </bean>
    
        <!-- controller -->
        <bean id="filesForm" class="com.fileupload.web.FilesForm">
            <property name="filesService" ref="filesService"/>
        </bean>
    
        <bean id="filesController" class="com.fileupload.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>

  • #2
    StrackTrace: while uploading something.....

    java.lang.IllegalArgumentException: No Statement specified
    at org.springframework.util.Assert.notNull(Assert.jav a:112)
    at org.springframework.jdbc.datasource.DataSourceUtil s.applyTimeout(DataSourceUtils.java:250)
    at org.springframework.jdbc.core.JdbcTemplate.applySt atementSettings(JdbcTemplate.java:1203)
    at org.springframework.jdbc.core.JdbcTemplate.execute (JdbcTemplate.java:586)
    at org.springframework.jdbc.core.JdbcTemplate.update( JdbcTemplate.java:792)
    at org.springframework.jdbc.core.JdbcTemplate.update( JdbcTemplate.java:815)
    at com.fileupload.service.FilesService.save(FilesServ ice.java:77)
    at com.fileupload.web.FilesController.upload(FilesCon troller.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.servlet.mvc.multiaction.Mu ltiActionController.invokeNamedMethod(MultiActionC ontroller.java:471)
    at org.springframework.web.servlet.mvc.multiaction.Mu ltiActionController.handleRequestInternal(MultiAct ionController.java:408)
    at org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(AbstractController.java:153)
    at org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:48)
    at org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:763)
    at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:709)
    at org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:613)
    at org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:536)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:852)
    at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run( JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)

    Comment

    Working...
    X