Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java EE (J2EE) и Spring > html form'a и загрузка картинки (spring)


Автор: xul 22.6.2008, 17:56
Добрый день
Возникла проблема с загрузкой файла на сервер.
Нашел несколько примеров. делаю то же, но результата нет.
вкратце
app-servlet.xml
Код
        <bean id="fileUpload" class="controllers.FileUploadController">
        <property name="commandClass" value="domain.FileUpload"/>
        <property name="formView" value="/view/file/fileview.jsp"/>
         <property name="successView" value="/view/file/fileview.jsp"/>
    </bean>
....
       <bean id="multipartResolver"   class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
.....
       <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/view/file/fileview.json">fileUpload</prop>
            </props>
        </property>
     </bean>

fileUpload.jsp
Код
         <form method="post" action="../view/file/fileview.json" enctype="multipart/form-data" name="formFotoUpload" >
        <label>Изображение:</label>
        <input type="file" name="file" id="fotoItems" />
        <input type="submit" value="Upload"  class="button" id="fotoItemsSubmit" />
    </form>

FileUploadController
Код
public class FileUploadController extends SimpleFormController {

    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws ServletException, IOException{
        System.out.println("initBinder");
        binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
    }
    
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,Object command, BindException errors)throws Exception{
        System.out.println("onSubmit");
        FileUpload bean = (FileUpload) command;
        byte[] bytes = bean.getFile();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");
        String uploadDir = getServletContext().getRealPath("/upload/");
        File dirPath = new File(uploadDir);
        if (!dirPath.exists())
          dirPath.mkdirs();
        String sep = System.getProperty("file.separator");
        File uploadedFile = new File(uploadDir + sep + file.getOriginalFilename());
        FileCopyUtils.copy(bytes, uploadedFile);
        request.getSession().setAttribute("message", "Upload completed.");
        String url = request.getContextPath() + "/gallery/" + file.getOriginalFilename();
        Map<String, String> model = new HashMap<String, String>();
        model.put("filename", file.getOriginalFilename());
        model.put("url", url);
        return new ModelAndView(getSuccessView(), "model", model);
    }

}

fileUpload.java
Код
public class FileUpload{

    private byte[] file;
    
    public void setFile(byte[] file){
        this.file = file;
    }
    
    public byte[] getFile(){
        return file;
    }
    
}

Как и говорил при выборе файла и нажатия на кнопка upload выводится только initBinder и ничего не просиходит.
Не могу понять почему.
Буду признателен за помощь
Благодарю

Автор: xul 22.6.2008, 18:28
решение моего вопроса =)
http://nroff.livejournal.com/1572.html

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)