Server-side implementation of File upload and download

Source: Internet
Author: User

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

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.