springmvc批量上传图片或文件和其他数据,上传到共享文件夹

@RequestMapping(value = "saveFeedback", method = RequestMethod.POST)
@ResponseBody
public Map saveFeedback(@RequestParam("textMess") String content, 
        @RequestParam("userId") String userid, 
        HttpServletRequest request){
    Map data = new HashMap();
    //根据userid查询用户名保存到功能反馈bean中
    TPatientUser tPatientUser = mobileDbService.getTPatientUserByParams(null,null,null,userid);
    FeedBackModel feedback = new FeedBackModel();
    feedback.setPid(null);
    feedback.setDeleted(false);
    feedback.setContent(content);
    feedback.setFrom_user_id(userid);
    feedback.setFrom_user_name(tPatientUser.getUsername());
    feedback.setCreate_time(new Date());
    try{
        if (request instanceof MultipartHttpServletRequest) {
            Properties prop = new Properties();// 属性集合对象
            String path = request.getSession().getServletContext().getRealPath("/WEB-INF/dorado-home/");
            FileInputStream fis = new FileInputStream(path+"/file.properties");
            prop.load(fis);// 将属性文件流装载到Properties对象中
            String filePath = prop.getProperty("filePath");
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
            Map fileMap = multipartHttpServletRequest.getFileMap();
            //Map.Entry entry = fileMap.entrySet().iterator().next(); 
            String fileNametotal = "";
            for(Map.Entry entry : fileMap.entrySet()){
                MultipartFile file = entry.getValue(); 
                InputStream is = file.getInputStream();
		String fileName = System.currentTimeMillis()+"."+file.getContentType().split("/")[1];
		PublicUti.smbPut(filePath+"feedback", is, fileName);
		if("".equals(fileNametotal)){
		    fileNametotal = "feedback/"+fileName;
		}else{
		    fileNametotal = fileNametotal+",feedback/"+fileName;
		}
	    }
	    feedback.setImage_url(fileNametotal);
	    data.put("path", feedback.getImage_url());
	}
	mobileDbService.saveFeedback(feedback);
	data.put("success", true);
	return data;
    }catch(IOException e){
	 e.printStackTrace();
	data.put("success", false);
	return data;
    }
}

    /** 
     * Description: 从本地上传文件到共享目录 
     * @param remoteUrl 
     *            共享文件目录 
     * @param localFilePath 
     *            本地文件绝对路径 
     */  
    public  static boolean smbPut(String remoteUrl, String localFilePath) {  
        InputStream in = null;  
        OutputStream out = null;  
        try {  
            File localFile = new File(localFilePath);  
            String fileName = localFile.getName();  
            SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName);  
            in = new BufferedInputStream(new FileInputStream(localFile));  
            out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));  
            byte[] buffer = new byte[1024];  
            while (in.read(buffer) != -1) {  
                out.write(buffer);  
                buffer = new byte[1024];  
            }  
            return true;  
        } catch (Exception e) {  
            LogUtil.error(e);  
            return false;  
        } finally {  
            try {  
                out.close();  
                in.close();  
            } catch (IOException e) {  
                LogUtil.error(e);  
            }  
        }  
    } 

你可能感兴趣的:(代码积累)