import Java.io.BufferedReader;
import java.io.File;
import Java.io.FileOutputStream;
import Java.io.InputStream;
import Java.io.InputStreamReader;
import Java.io.OutputStream;
import Java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
/**
* File Upload and download
* @author 20170323
*
*/
Public class Filenetuploadanddownload {
/**
* File Upload
* @param String File upload path
* @param String file Local Path
* @return
* @throws Exception
*/
Public String FileUpload (String urlpath,string filelocation) throws exception{
String res = "";
Establish a connection client
HttpClient HttpClient = new defaulthttpclient ();
Connecting to a server
HttpPost HttpPost = new httppost (URLPath);
Loading files
Filebody file = new filebody (new file (filelocation));
Initialize the loader?
Multipartentity reqentity = new multipartentity ();
Loading files
Reqentity.addpart ("file", file);
Load Loader
Httppost.setentity (reqentity);
Ready to send data (?? )
HttpResponse response = Httpclient.execute (HttpPost);
Ready to send data (?? )
Httpentity httpentity = response.getentity ();
Generate flow
BufferedReader br = new bufferedreader (new InputStreamReader (Httpentity.getcontent (), "UTF-8"));
StringBuffer backdata = new stringbuffer ();
String line = null;
Send
while (line = Br.readline ()) = null) {
Backdata.append (line);
}
res = backdata.tostring ();
System. out. println (res);
return Res;
}
/**
* File Download
* @param String Download path
* @param String file name
* @param String Local Storage path
* @return
* @throws Exception
*/
Public String FileDownload (String filenetpath,string filename,string Upfilepath) throws exception{
Connecting to a server
URL url = new URL (filenetpath);
Generate flow
InputStream is = Url.openstream ();
Generate local file
Upfilepath + = "/" +filename;
File Outflie = new file (Upfilepath);
if (!outflie.getparentfile (). exists ()) {
Outflie.getparentfile (). mkdir ();
}
if (!outflie.exists ()) {
Outflie.createnewfile ();
}
OutputStream output = new fileoutputstream (Outflie);
int temp = 0;
Write file
while (temp = Is.read ())! =-1) {
Output.write (temp);
}
Is.close ();
Output.close ();
System. out. println (IS);
return Upfilepath;
}
}
Server-side implementation of File upload and download