SpringMVC图片上传

jsp前台代码:

<form id="formid" action="uploadpictrue.do"enctype="multipart/form-data" method="post">
    <input id="file-0b"  type="file" name="file"/>
    <input type="submit" value="上传" name="submit">
</form

controller中的代码:

public class UploadpictureContrnller {

    /**
     * 字节流上传图片
     * @param request
     * @param response
     */
   
    @RequestMapping(value="uploadpictrue.do",method=RequestMethod.POST)
    @ResponseBody
    public void uploadpictrue(HttpServletRequest request,HttpServletResponse response) {
         
        MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request;
       
        MultipartFile file = multipart.getFile("file");
//        // 获得文件名:  
//        //获得保存路径
        String path = request.getSession().getServletContext().getRealPath("down\\uploadpictrue");
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date day = new Date();
        String folder = fmt.format(day);
        String savepath = path+"\\"+folder;
        File savefile = new File(savepath);
        //判断文件夹是否存在不存在则创建
        if(!savefile.exists()&& !savefile.isDirectory()){
            savefile.mkdir();
        }
        String filename = file.getOriginalFilename();
        //获得出流
        FileOutputStream fos = null;
        ServletInputStream inps = null;
        //保存文件
        try {
            fos = new FileOutputStream(savepath+"\\"+filename);
            inps = multipart.getInputStream();
            byte[] b = new byte[1024];
            int i =0;
            inps.readLine(b, 0, b.length);
            while((i=inps.readLine(b, 0, b.length))!=-1){
                fos.write(b, 0, i);
                fos.flush();
            }
            String[] suffixs = filename.split("[.]");
            String suffix = suffixs[suffixs.length-1];
            UUID uuid = UUID.randomUUID();
            String uu = uuid.toString().replace("-","");
            String newname = uu+"."+suffix;
           
            if(fos!=null){
                fos.close();
            }
            File f = new File(savepath+"\\"+filename);
            if(f.exists()&&f.isFile()){
                File f2 =new File(savepath+"\\"+newname);
            boolean falg = f.renameTo(f2);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(inps!=null){
                try {
                    inps.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
       
    }
    /**
     * 字符流上传图片
     * @param request
     * @param response
     */
    @RequestMapping(value="uploadpictrue.do",method=RequestMethod.POST)
    @ResponseBody
    public void uploadpictrue2(HttpServletRequest request,HttpServletResponse response) {
         
        MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request;
       
        MultipartFile file = multipart.getFile("file");
//        // 获得文件名:  
//        //获得保存路径
        String path = request.getSession().getServletContext().getRealPath("down\\uploadpictrue");
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date day = new Date();
        String folder = fmt.format(day);
        String savepath = path+"\\"+folder;
        File savefile = new File(savepath);
        //判断文件夹是否存在不存在则创建
        if(!savefile.exists()&& !savefile.isDirectory()){
            savefile.mkdir();
        }
        String filename = file.getOriginalFilename();
        //获得出流
        BufferedReader bu =null;
        OutputStream os = null;
        PrintWriter pw;
        //保存文件
        try {
            bu = new BufferedReader(new InputStreamReader(multipart.getInputStream()));
            os = new FileOutputStream(savepath+"\\"+filename);
            pw = new PrintWriter(os);
            String str ="";
            while((str=bu.readLine())!=null){
                pw.write(str);
                pw.flush();
            }
            String[] suffixs = filename.split("[.]");
            String suffix = suffixs[suffixs.length-1];
            UUID uuid = UUID.randomUUID();
            String uu = uuid.toString().replace("-","");
            String newname = uu+"."+suffix;
           
            if(os!=null){
                os.close();
                os=null;
            }if(pw!=null){
                pw.close();
                pw=null;
            }
            File f = new File(savepath+"\\"+filename);
            if(f.exists()&&f.isFile()){
                File f2 =new File(savepath+"\\"+newname);
            boolean falg = f.renameTo(f2);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(bu!=null){
                try {
                    bu.close();
                    bu=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
       
    }
}

Spring-mvc.xml中配置:

<!--MultipartResolver 文件上传 -->
    <!-- 文件上传通用的多部分解析器 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"></property>
        <property name="maxUploadSize" value="10485760000"></property>
        <property name="maxInMemorySize" value="40960"></property>
    </bean>

猜你喜欢

转载自kisslongge.iteye.com/blog/2312631