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

@RequestMapping(value = "saveFeedback", method = RequestMethod.POST)
@ResponseBody
public Map<String,Object> saveFeedback(@RequestParam("textMess") String content, 
        @RequestParam("userId") String userid, 
        HttpServletRequest request){
    Map<String, Object> data = new HashMap<String, Object>();
    //根据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<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
            //Map.Entry<String, MultipartFile> entry = fileMap.entrySet().iterator().next(); 
            String fileNametotal = "";
            for(Map.Entry<String, MultipartFile> 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);  
            }  
        }  
    } 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java 基于springMVC图片,MySQL源码 @Controller @RequestMapping("/upload") public class UploadFileController { @Autowired private UploadFileService uploadFileService; @RequestMapping("/upfile") public String upload(HttpServletRequest request, @RequestParam("designation") String designation, @RequestParam("remark") String remark, //@RequestParam("file") String file, Model model)throws Exception { String str = designation; String[] st=str.split(","); //以逗号为分隔符进行截取 String re = remark; String[] stre = re.split(","); UploadFile uploadFile = new UploadFile(); //转成文件请求 MultipartHttpServletRequest murequest = (MultipartHttpServletRequest)request; //在文件请求中获取文件,根据file的name List files = murequest.getFiles("image"); if( files !=null && files.size()>0) { for(int i=0; i<files.size(); i++) { String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase()+"_"; String filename = files.get(i).getOriginalFilename(); //System.out.println("filename="+filename); int hCode =filename.hashCode(); String hex = Integer.toHexString(hCode); String mkdir = hex.charAt(0)+"\\"+hex.charAt(1)+"\\"; String path = request.getServletContext().getRealPath("/images/")+mkdir; //System.out.println("path="+path); File filepath = new File(path,filename); if(!filepath.getParentFile().exists()) { filepath.getParentFile().mkdirs(); } //将上文件保存到目标文件中 files.get(i).transferTo(new File(path+File.separator+filename)); //获取数据库存储路径 String root = request.getContextPath(); String mkdirsql = hex.charAt(0)+"/"+hex.charAt(1)+"/"; String sqlpath = root+"/images/"+mkdirsql+filename; String imgpath = sqlpath; //图片保存到数据库的路径 uploadFile.setDesignation(st[i]); uploadFile.setRemark(stre[i]); uploadFile.setFile(filename); uploadFile.setImgpath(sqlpath); uploadFileService.addUploadFile(uploadFile); } return "success"; } return "404"; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值