图片上传工具类

package com.hr.common.utils;

import org.springframework.web.multipart.MultipartFile;

import com.hr.common.pojo.PictureResult;

/**
*
* @ClassName PictureUtils
* @Description TODO(图片上传工具类 上传图片并返回状态以及路径)
* @author Administrator
* @Date 2018年8月22日 下午7:46:20
* @version 1.0.0
*/
public class PictureUtils {
public static PictureResult uploadImages(MultipartFile file) {
PictureResult picture = new PictureResult();
if(file != null) {
String filename=file.getOriginalFilename();
//System.out.println(“filename:”+filename);
//判断文件名是否为空
if(filename.trim().length()>0) {
//获取后缀名称,通过分隔符点
String substring = filename.substring(filename.lastIndexOf(“.”)+1);
// System.out.println(“后缀名是:”+substring);
try {
FastDFSClient dfClient=new FastDFSClient(“classpath:client.conf”);
//返回上传路径,把图片序列化到字节
String uploadFile = dfClient.uploadFile(file.getBytes(),substring);

            //  System.out.println("图片上传全路径"+uploadFile);

            //  System.out.println("图片上传全路径"+uploadFile);
        System.out.println("filename:"+filename);
        //判断文件名是否为空
                picture.setUrl(uploadFile);
                picture.setCode(0);
                picture.setMes("图片上传成功");
            } catch (Exception e) {
                picture.setCode(1);
                picture.setMes("图片上传失败");
            }
        }

    }else {
        picture.setCode(1);
        picture.setMes("图片为空");
    }
    return picture;     
}

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值