天天看點

web項目使用fastdsf上傳|下載下傳檔案

在上傳代碼中添加一下代碼

suffix=suffix.substring(1);

fast.FastDFSFile file = new fast.FastDFSFile(mFile.getBytes(), suffix);

NameValuePair[] meta_list = new NameValuePair[4];

meta_list[0] = new NameValuePair("fileName", mFile.getOriginalFilename());

meta_list[1] = new NameValuePair("fileLength", String.valueOf(mFile.getSize()));

meta_list[2] = new NameValuePair("fileExt", suffix);

meta_list[3] = new NameValuePair("fileAuthor", "WangLiang");

String filePath = fast.FileManager.upload(file, meta_list);

System.out.println(filePath);

web項目使用fastdsf上傳|下載下傳檔案

public class FastDFSClient {

private TrackerClient trackerClient = null;

private TrackerServer trackerServer = null;

private StorageClient1 storageClient = null;

private StorageServer storageServer = null;

public FastDFSClient(String conf) throws Exception {

if (conf.contains("classpath:")) {

conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());

}

ClientGlobal.init(conf);

trackerClient = new TrackerClient();

trackerServer = trackerClient.getConnection();

storageServer = null;

storageClient = new StorageClient1(trackerServer, storageServer);

public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {

String result = storageClient.upload_file1(fileName, extName, metas);

return result;

public String uploadFile(String fileName) throws Exception {

String result = storageClient.upload_file1(fileName, null, null);

public String uploadFile(String fileName, String extName) throws Exception {

String result = storageClient.upload_file1(fileName, extName, null);

public class FastDFSFile {

private static final long serialVersionUID = 1L;

private byte[] content;

private String name;

private String ext;

private String length;

public FastDFSFile(byte[] content, String ext) {

this.content = content;

this.ext = ext;

public FastDFSFile(byte[] content, String name, String ext) {

this.name = name;

public FastDFSFile(byte[] content, String name, String ext, String length

) {

this.length = length;

public byte[] getContent() {

return content;

public void setContent(byte[] content) {

public String getName() {

return name;

public void setName(String name) {

public String getExt() {

return ext;

public void setExt(String ext) {

public String getLength() {

return length;

public void setLength(String length) {

public class FileManager implements FileManagerConfig {

private static TrackerClient trackerClient;

private static TrackerServer trackerServer;

private static StorageServer storageServer;

private static StorageClient storageClient;

static {

try {

String classPath = new File(FileManager.class.getResource("/").getFile()).getCanonicalPath();

String fdfsClientConfigFilePath = classPath + File.separator + CLIENT_CONFIG_FILE;

ClientGlobal.init(fdfsClientConfigFilePath);

storageClient = new StorageClient(trackerServer, storageServer);

} catch (Exception e) {

e.printStackTrace();

/**

* 檔案上傳

* @param file

* @param valuePairs

* @return

*/

public static String upload(FastDFSFile file, NameValuePair[] valuePairs) {

String[] uploadResults = null;

uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), valuePairs);

String groupName = uploadResults[0];

String remoteFileName = uploadResults[1];

String fileAbsolutePath = PROTOCOL + TRACKER_NGNIX_ADDR + SEPARATOR + groupName + SEPARATOR + remoteFileName;

return fileAbsolutePath;

* 檔案下載下傳

* @param groupName

* @param remoteFileName

* @param specFileName

public static ResponseEntity<byte[]> download(String groupName, String remoteFileName, String specFileName) {

byte[] content = null;

HttpHeaders headers = new HttpHeaders();

content = storageClient.download_file(groupName, remoteFileName);

headers.setContentDispositionFormData("attachment",

new String(specFileName.getBytes("UTF-8"), "iso-8859-1"));

headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

// TODO Auto-generated catch block

return new ResponseEntity<byte[]>(content, headers, HttpStatus.CREATED);

public interface FileManagerConfig extends Serializable {

public static final String PROTOCOL = "http://";

public static final String SEPARATOR = "/";

public static final String TRACKER_NGNIX_ADDR = "192.168.111.136";

public static final String TRACKER_NGNIX_PORT = "";

public static final String CLIENT_CONFIG_FILE = "client.conf";

web項目使用fastdsf上傳|下載下傳檔案
web項目使用fastdsf上傳|下載下傳檔案

tracker_server=192.168.111.136:22122

下載下傳

根據之前上傳儲存的實體路徑 截取

* 下載下傳 截取實體路徑下載下傳

* @param awardModel

* @param request

* @param response

* @param createtime

* @param endtime

* @param act_id

* @throws Exception

/*@RequestMapping(value = "/{username}/download", method = RequestMethod.GET)

public ResponseEntity<byte[]> download(@PathVariable String username, Model model,HttpServletResponse response) throws IOException, MyException {

User u = users.get(username);

String filePath = u.getFilePath();

String substr = filePath.substring(filePath.indexOf("group"));

String group = substr.split("/")[0];

String remoteFileName = substr.substring(substr.indexOf("/")+1);

String specFileName = username + substr.substring(substr.indexOf("."));

return FileManager.download(group, remoteFileName,specFileName);

}*/