Web project using FASTDSF upload | download file

Source: Internet
Author: User

Add code in the upload code

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

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);
return result;
}

public string UploadFile (string fileName, String extname) throws Exception {
String result = Storageclient.upload_file1 (fileName, extname, NULL);
return result;
}
}

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.content = content;
THIS.name = name;
This.ext = ext;
}

Public Fastdfsfile (byte[] content, string name, string ext, string length
) {
this.content = content;
THIS.name = name;
This.ext = ext;
this.length = length;
}

Public byte[] GetContent () {
return content;
}

public void SetContent (byte[] content) {
this.content = content;
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

Public String Getext () {
return ext;
}

public void Setext (String ext) {
This.ext = ext;
}

Public String GetLength () {
return length;
}

public void SetLength (String length) {
this.length = length;
}


}

public class FileManager implements Filemanagerconfig {

Private static final long serialversionuid = 1L;
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);

Trackerclient = new Trackerclient ();
Trackerserver = Trackerclient.getconnection ();

Storageclient = new Storageclient (Trackerserver, storageserver);

} catch (Exception e) {
E.printstacktrace ();
}
}


/**
* File Upload
* @param file
* @param valuepairs
* @return
*/
public static String upload (fastdfsfile file, namevaluepair[] valuepairs) {
string[] uploadresults = null;
try {
Uploadresults = Storageclient.upload_file (File.getcontent (), File.getext (), valuepairs);
} catch (Exception e) {
E.printstacktrace ();
}
String groupName = uploadresults[0];
String remotefilename = uploadresults[1];

String Fileabsolutepath = PROTOCOL + tracker_ngnix_addr + SEPARATOR + groupName + SEPARATOR + remotefilename;
return fileabsolutepath;
}

/**
* File Download
* @param groupName
* @param remotefilename
* @param specfilename
* @return
*/
public static responseentity<byte[]> Download (string groupName, String remotefilename, String specfilename) {
byte[] content = null;
Httpheaders headers = new Httpheaders ();
try {
Content = Storageclient.download_file (GroupName, remotefilename);
Headers.setcontentdispositionformdata ("Attachment",
New String (Specfilename.getbytes ("UTF-8"), "iso-8859-1"));
Headers.setcontenttype (Mediatype.application_octet_stream);
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
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";
}

tracker_server=192.168.111.136:22122

Download

Interception based on the physical path of the previously uploaded save

/**
* Download intercept physical path download
* @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);
}*/

Web project using FASTDSF upload | download file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.