fastdfs 图片服务器 使用java端作为客户端上传图片

之前有说道搭建fastdfs作为图片服务器,但是没有说明如何真正在代码里调用,那么今天大致讲一下,如何使用java客户端进行上传

首先你得要有一个客户端,导入到eclipse中即可

git地址如下:

https://github.com/leechenxiang/fastdfs-client-java

js:

$(function () {
    $('#fileupload').fileupload({
        dataType: 'json',
        done: function (e, data) {
            console.log(data);  
            if (data.result.status != "200") {
                alert(data.result.msg);
            } else {
                $.each(data.result.data, function (index, file) {
                  $("#lee").html("<a href='" + file.url + "' target='_blank'><img src='" + file.url + "' width='80' hight='80'></img></a>");
                  $("#picUrl").attr("value", file.urlDB);
              }); 
            }
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .bar').css(
                'width',
                progress + '%'
            );
           },
        dropZone: $('#dropzone')
    });
});

html:

<input id="fileupload" type="file" name="files[]" data-url="/file/upload.action" multiple>
    
    <div id="dropzone" class="fade well">Drop files here</div>
    
    <div id="progress" class="progress">
        <div class="bar" style="width: 0%;"></div>
    </div>
    
    <div id="lee"></div>

controller:

@RequestMapping(value="/upload", method = RequestMethod.POST)
    @ResponseBody
    public LeeJSONResult upload(MultipartHttpServletRequest request, HttpServletResponse response) throws Exception {

        List<PictureResult> picList = new ArrayList<PictureResult>();
        
        Iterator<String> itr = request.getFileNames();
        MultipartFile mpf = null;
        while (itr.hasNext()) {
            mpf = request.getFile(itr.next());
            PictureResult pic = pictureService.uploadPic(mpf);
            picList.add(pic);
        }
        
        return LeeJSONResult.ok(picList);
    }

service:

@Value("${IMAGE_SERVER_BASE_URL}")
    private String IMAGE_SERVER_BASE_URL;
    
    @Value("${SERVER_PATH}")
    private String SERVER_PATH;
    
    @Override
    public PictureResult uploadPic(MultipartFile picFile) {
        PictureResult result = new PictureResult();
        // 判断图片是否为空
        if (picFile.isEmpty()) {
            result.setError(YesOrNo.YES.value);
            result.setMessage("图片为空");
            return result;
        }
        // 上传到图片服务器
        try {
            // 取图片扩展名
            String originalFilename = picFile.getOriginalFilename();
            // 取扩展名不要“.”
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            FastDFSClient client = new FastDFSClient("classpath:resource/client.conf");
            String url = client.uploadFile(picFile.getBytes(), extName);
            // 把url响应给客户端
            result.setError(YesOrNo.NO.value);
            result.setUrlDB(url);
            result.setUrl(IMAGE_SERVER_BASE_URL + url);
        } catch (Exception e) {
            e.printStackTrace();
            result.setError(YesOrNo.YES.value);
            result.setMessage("图片上传失败");
        }
        return result;
    }

最后配置:

tracker_server=192.168.1.188:22122

 

posted @ 2017-08-07 16:42  风间影月  阅读(1286)  评论(0编辑  收藏  举报