parent
c67e86a6ff
commit
2df0f4761f
@ -0,0 +1,111 @@
|
||||
package com.example.musicwork.freeModel;
|
||||
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class FileServiceDownload {
|
||||
|
||||
public static boolean downloadForm(String filename) throws IOException {
|
||||
//String downloadDir = Environment.getExternalStorageDirectory();
|
||||
// 普通参数
|
||||
//String filename="111.txt";
|
||||
String urlStr = "http://10.0.2.2:8080/Manager/readfil-servlet";
|
||||
MyThread_filedownload myThread = new MyThread_filedownload(filename,urlStr);
|
||||
try
|
||||
{
|
||||
myThread.start();
|
||||
myThread.join();
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return myThread.getResult();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
class MyThread_filedownload extends Thread {
|
||||
|
||||
private String urlStr;
|
||||
private String filename;
|
||||
private String downloadDir;
|
||||
public boolean result=false;
|
||||
|
||||
public MyThread_filedownload(String filename, String urlStr) {
|
||||
this.urlStr = urlStr;
|
||||
this.filename = filename;
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
URL url = new URL(urlStr);
|
||||
URLConnection urlConnection = url.openConnection();
|
||||
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
|
||||
httpURLConnection.setRequestMethod("POST");
|
||||
httpURLConnection.setRequestProperty("Charset", "UTF-8");
|
||||
String data = "filename=" + URLEncoder.encode(filename, "utf-8");//设置数据
|
||||
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//设置响应类型
|
||||
httpURLConnection.setRequestProperty("Content-Length", data.length() + "");//设置内容长度
|
||||
httpURLConnection.setDoOutput(true);//允许输出
|
||||
OutputStream outputStream = httpURLConnection.getOutputStream();
|
||||
outputStream.write(data.getBytes("utf-8"));//写入数据
|
||||
httpURLConnection.connect();
|
||||
// 文件大小
|
||||
int fileLength = httpURLConnection.getContentLength();
|
||||
URLConnection con = url.openConnection();
|
||||
InputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
|
||||
|
||||
Log.e("download filename:",filename);
|
||||
String filepath=Environment.getExternalStorageDirectory().getPath();
|
||||
File file=new File(filepath,"/Music/"+filename);
|
||||
if (!file.getParentFile().exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
}
|
||||
OutputStream out = new FileOutputStream(file);
|
||||
int size = 0;
|
||||
byte[] buf = new byte[1024];
|
||||
while ((size = bin.read(buf)) != -1) {
|
||||
out.write(buf, 0, size);
|
||||
}
|
||||
IOUtils.copy(bin, out);
|
||||
if(file.length()!=0){
|
||||
result=true;
|
||||
}
|
||||
bin.close();
|
||||
out.close();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public boolean getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in new issue