springmvc文件上传

private static final String uploadFilePath = "d:\\temp_upload_file\\";  

/** 

 * 新增 - 提交 – 只保存文件到服务器上 

 */  

@RequestMapping(value = "addAction.do")  

public String add_action(ModelMap model, StudentForm form) {  

try {  

        MultipartFile uploadFile = form.getStudentPhoto();  

        String filename = uploadFile.getOriginalFilename();  

        InputStream is = uploadFile.getInputStream();  

        // 如果服务器已经存在和上传文件同名的文件,则输出提示信息  

        File tempFile = new File(uploadFilePath + filename);  

        if (tempFile.exists()) {  

            boolean delResult = tempFile.delete();  

            System.out.println("删除已存在的文件:" + delResult);  

        }  

        // 开始保存文件到服务器  

        if (!filename.equals("")) {  

            FileOutputStream fos = new FileOutputStream(uploadFilePath + filename);  

            byte[] buffer = new byte[8192]; // 每次读8K字节  

            int count = 0;  

            // 开始读取上传文件的字节,并将其输出到服务端的上传文件输出流中  

            while ((count = is.read(buffer)) > 0) {  

                fos.write(buffer, 0, count); // 向服务端文件写入字节流  

            }  

            fos.close(); // 关闭FileOutputStream对象  

            is.close(); // InputStream对象  

        }  

    } catch (FileNotFoundException e) {  

        e.printStackTrace();  

    } catch (IOException e) {  

        e.printStackTrace();  

    }  

}  

posted @ 2016-06-06 16:01  斯蒂卡  阅读(57)  评论(0编辑  收藏  举报