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;
}
}