FastDFSUtil

fdfs_client.conf配置如下:

connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 80
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 192.168.36.159:22122

==================================================

String imgPath = "D://twoDimensionCode/"+ goodsId + ".png";

FastDFSUtil fastDFSUtil = new FastDFSUtil();
String[] fastDFSImgPath = fastDFSUtil.fastDFSUploadFolder(imgPath);
if(fastDFSImgPath.length == 3){
GoodsInfo goodsInfo = goodsManagementService.getGoodsInfoById(goodsId);
goodsInfo.setTdcImg(fastDFSImgPath[2] + "/" + fastDFSImgPath[0]);
goodsManagementService.saveUpdateGoodsInfo(goodsInfo);
}


package com.lenovo.shop.util;



import java.io.File;
import java.io.FileInputStream;
import java.util.Date;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;


public class FastDFSUtil {
private static TrackerClient trackerClient = null;
private static TrackerServer trackerServer = null;

private static StorageServer storageServer = null;
private static StorageClient storageClient = null;
private static final Log logger = LogFactoryImpl
           .getLog(FastDFSUtil.class);

public FastDFSUtil(){
String path = GetProperties.getValue("fastConf")+"fdfs_client.conf";
try {
ClientGlobal.init(path);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageClient = new StorageClient(trackerServer, storageServer);
} catch (Exception e) {


}

/**
* 上传到fastDFS

*/
public String[] fastDFSUploadFolder(String folder) throws Exception{
File file = new File(folder);
   String name_ext[] = fastDFSUploadFile(file);
return name_ext;
}

/**
* 上传到fastDFS

*/
public String[] fastDFSUploadFile(File file) throws Exception{
logger.info("----fastdfsUtil ----file ="+file);
FileInputStream fis=new FileInputStream(file);
logger.info("----fastdfsUtil ----fis ="+fis);
byte[] file_buff = null;
   if(fis != null){
    int len = fis.available();
    file_buff = new byte[len];
    fis.read(file_buff);
   }
   String name_ext[] = fastDFSUpload(file_buff);
   logger.info("----fastdfsUtil ----name_ext[] ="+name_ext);
   fis.close();
return name_ext;
}

/**
* 上传到fastDFS返回图片路径、后缀名、分组信息

*/
public String[] fastDFSUpload(byte[] data) throws Exception{
//上传后返回的图片名称以及图片后缀名
    String name_ext[] = new String[3];
   
   NameValuePair[] meta_list = new NameValuePair[1];
    try {
meta_list[0] = new NameValuePair("author", Provider.getCurrentUserAccount());
} catch (Exception e) {
meta_list[0] = new NameValuePair("date", String.valueOf(((new Date()).getTime())));
}
   
//    meta_list[0] = new NameValuePair("author", Provider.getCurrentUserAccount());
   
   byte[] file_buff = data;
   
   String group_name = null;
   logger.info("----fastdfsUtil ----storageClient ="+storageClient);
   String[] results = storageClient.upload_file(file_buff, "jpg", meta_list);


   if (results == null){
       results = storageClient.upload_file(file_buff, "jpg", meta_list);
       if (results == null){
        logger.error("upload file fail, error code: " + storageClient.getErrorCode());
        return null;
       }
   }
   
   group_name = results[0];
   String remote_filename = results[1];
   if(!"".equals(remote_filename)){
    String ext = remote_filename.substring(remote_filename.lastIndexOf('.')+1);
    String name = remote_filename.substring(0,remote_filename.lastIndexOf('.'));
    name_ext[0] = name;
    name_ext[1] = ext;
    name_ext[2] = group_name;
   }
return name_ext;
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
org.csource.common.MyException: getStoreStorage fail, errno code: 2 at org.csource.fastdfs.StorageClient.newReadableStorageConnection(StorageClient.java:1767) at org.csource.fastdfs.StorageClient.download_file(StorageClient.java:1219) at org.csource.fastdfs.StorageClient.download_file(StorageClient.java:1206) at com.wzdigit.framework.utils.FastDFSUtil.downFile(FastDFSUtil.java:209) at com.wzdigit.srm.dsr.utils.FileUtil.getSingleFile(FileUtil.java:51) at com.wzdigit.srm.dsr.service.bidding.BiddingorderService.getVendorQuotation(BiddingorderService.java:796) at com.wzdigit.srm.dsr.service.bidding.BiddingorderService.sendEmail(BiddingorderService.java:746) at com.wzdigit.srm.dsr.service.bidding.BiddingorderService$$FastClassBySpringCGLIB$$ebfcbd5a.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at com.alibaba.druid.support.spring.stat.DruidStatInterceptor.invoke(DruidStatInterceptor.java:73) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691) at com.wzdigit.srm.dsr.service.bidding.BiddingorderService$$EnhancerBySpringCGLIB$$80ace30.sendEmail(<generated>) at com.wzdigit.srm.dsr.service.bidding.BiddingorderService$$FastClassBySpringCGLIB$$ebfcbd5a.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值