Spring 3文件上传使用CommonsMultipartResolver无法正常工作

时间:2018-03-04 11:26:50

标签: spring-mvc spring-3 apache-commons-fileupload

我使用CommonsMultipartResolver将word / pdf文件上传到postgres数据库,同时上传文件我使用file.getBytes()打印了byte []数组,它显示为

  

字节========================== [B @ 6b02547c

文件正在上传,但是当我下载文件时,我从ms-word收到以下错误:“由于内容存在问题,无法打开文件abc.docx”。请帮我解决这个问题。

@SuppressWarnings({ "unused", "static-access" })
    @RequestMapping( value="/RegisterCandidate" ,  method = RequestMethod.POST)
    private String RegisterCandidate(HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam CommonsMultipartFile[] fileUpload ) throws Exception{

        System.out.println("In method");
        String email = request.getParameter("email");
        System.out.println("email==============="+email);
        String Password = request.getParameter("password");
        String usr_name = request.getParameter("name");
        String mobile_no = request.getParameter("mobile_no");
        Date dateentry = new Date();
        java.sql.Timestamp entry_date = new Timestamp(dateentry.getTime()); 
        Users_Pojo usr  = new Users_Pojo(); 

        if (fileUpload != null && fileUpload.length > 0) {
            for (CommonsMultipartFile aFile : fileUpload){                             
                usr.setFilename(aFile.getOriginalFilename());
                usr.setFile_data(aFile.getBytes());


                System.out.println("aFile.getBytes()======"+aFile.getBytes());
                System.out.println("aFile.getInputStream()======"+aFile.getInputStream());
                System.out.println("aFile.getStorageDescription()======"+aFile.getStorageDescription());
                System.out.println("aFile.getSize();======"+aFile.getSize());
                System.out.println("aFile.getContentType();==="+aFile.getContentType());/*  */


            }
        }


        usr.setUc_password("ex123");
        usr.setUc_name(email);
        usr.setUc_contact_person(email);
        usr.setUc_phone_no(BigInteger.valueOf(Long.parseLong(mobile_no)));
        usr.setUc_email_id(email);
        usr.setUc_type_id(1);
        usr.setUc_active(1);
        usr.setValid_from(null);
        usr.setValid_to(null);
        usr.setDesignation("jobseekar");
        usr.setIp_address("164.100.200.179");
        usr.setUser_location(1);
        usr.setEntry_date(entry_date);

        scm_service.save(usr, email);

        /*System.out.println("email==="+email);
        System.out.println("Password==="+Password);
        System.out.println("usr filename==="+usr.getFilename());*/
        return "success";
        //return "redirect:Login.html"; 
    }

配置:

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

使用以下方式下载:

@RequestMapping(value = "/download",method = RequestMethod.GET)
    public void getAttachmenFromDatabase( HttpServletRequest request,HttpServletResponse response){
        response.setContentType("application/vnd.ms-word");

        String resume_id = request.getParameter("resume_id");

        long attachid = Long.parseLong(resume_id);

        try {

            Users_Pojo file_attachment = (Users_Pojo)  scm_service.getFiles(attachid);
            System.out.println("file_attachment.getFilename()======="+file_attachment.getFile_data());
            response.setHeader("Content-Disposition", "inline; filename=\""+ file_attachment.getFilename() +"\"");
            response.setContentLength(file_attachment.getFile_data().length);

            FileCopyUtils.copy(file_attachment.getFile_data(), response.getOutputStream());          
            response.flushBuffer();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

0 个答案:

没有答案