SpringMVC项目通过ueditor跨服务器通过ftp服务上传文件到jeecms网站

公司做项目,在SpringMVC项目下往jeecms网站程序底下上传文件,纠结了好久,终于尝试成功(每次只能上传一个文件);

由于俩个项目存在没在一个服务器的情况下,所以需要配置ftp服务上传到jeecms程序下;

SpringMVC获取的file格式为MultipartFile;

@RequestMapping(value = "/fjscurl", method = RequestMethod.POST)
    public void web_fjsc(HttpServletRequest request,
            HttpServletResponse response,MultipartFile file) throws ServletException, IOException {
        //获取网站部署ip+port
        String weburl=fastDFSConfig.getWeburl();
        //获取网站部署路径
        String webpath=fastDFSConfig.getWebpath();
        //获取网站上传目录
        String uploadpath=fastDFSConfig.getUploadPath();
        JsonObject results = new JsonObject();
        String fileName=file.getOriginalFilename();
        Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
        String month = String.valueOf(c.get(Calendar.MONTH)+1);
        String currentMonthFile = String.valueOf(c.get(Calendar.YEAR))+(month.length()==1?"0"+month:month);//定义上传文件夹
        SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmssssss");//定义上传文件名,防止文件名乱码和文件重名
        String filena=df.format(new Date());
        String fileType=fileName.substring(fileName.lastIndexOf("."));
        String url=weburl+"/"+webpath+"/"+uploadpath+"/"+currentMonthFile+"/"+ filena+fileType;
        try {
            File f = null;  
            if(file.equals("")||file.getSize()<=0){  
                file = null;  
            }else{  
                InputStream ins = file.getInputStream();  
                f=new File(fileName);
                file.transferTo(f);
                inputStreamToFile(ins, f);  
                InputStream in = new FileInputStream(f);//将文件转为流传入工具类
                FtpUtils.uploadFile("/"+currentMonthFile, filena+fileType, in);//调用上传工具类
                //程序结束时,删除临时文件
                deleteFile(f);
                ins.close();

            }

//ueditor固定返回

      results.addProperty("fileType", fileType);

            results.addProperty("title", fileName);

            results.addProperty("original",fileName);
            results.addProperty("state", "SUCCESS");
            results.addProperty("url",url);
        } catch (Exception e) {
            log.error(e.getMessage());
            results.addProperty("success", false);
        }
        response.getWriter().write(results.toString());

    }

修改ueditor文件夹下ueditor.config.js上传路径

 //图片上传配置区
        ,imageUrl:"/fjsc"             //图片上传提交地址(外部传入)
        ,imagePath:""                    //图片修正地址,引用了fixedImagePath,如有特殊需求,可自行配置
        ,imageFieldName:"file"                  //图片数据的key,若此处修改,需要在后台对应文件修改对应参数
        //,compressSide:0                           //等比压缩的基准,确定maxImageSideLength参数的参照对象。0为按照最长边,1为按照宽度,2为按照高度
        //,maxImageSideLength:900                   //上传图片最大允许的边长,超过会自动等比缩放,不缩放就设置一个比较大的值,更多设置在image.html中
        ,savePath: ['/u']    //图片保存在服务器端的目录, 默认为空, 此时在上传图片时会向服务器请求保存图片的目录列表,
                                                            // 如果用户不希望发送请求, 则可以在这里设置与服务器端能够对应上的目录名称列表
                                                            //比如: savePath: [ 'upload1', 'upload2' ]

        //涂鸦图片配置区(后台暂未实现)
        ,scrawlUrl:APP_BASE+"/ueditor/scraw.do?Type=Image"            //涂鸦上传地址
        ,scrawlPath:""                            //图片修正地址,同imagePath

        //附件上传配置区
        ,fileUrl:"/fjscurl"              //附件上传提交地址
        ,filePath:""                   //附件修正地址,同imagePath
        ,fileFieldName:"file"                    //附件提交的表单名,若此处修改,需要在后台对应文件修改对应参数

猜你喜欢

转载自blog.csdn.net/qq_36397267/article/details/80507804
今日推荐