Androidプラットフォーム上のファイルダウンロード、ファイルとファイルの操作
Androidプラットフォームでのファイルの操作はjavaとほぼ同じです
気をつけてgetExternalStorageDirectory()+"/"で入手できるSDディレクトリがベスト
次はダウンロード
気をつけてgetExternalStorageDirectory()+"/"で入手できるSDディレクトリがベスト
package duohuo.teng.net;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.os.Environment;
public class FileUtil {
private String SDPATH;
public FileUtil() {
// SD
SDPATH=Environment.getExternalStorageDirectory()+"/";
}
/*
*
* */
public File createFile(String fileName) throws IOException{
File file=new File(SDPATH+fileName);
file.createNewFile();
return file;
}
/*
*
* */
public File createDirs(String dirName){
File file=new File(SDPATH+dirName);
Boolean b=file.mkdirs();
System.out.println(b);
return file;
}
/* ( )
*
* */
public boolean isFileExist(String path){
return new File(SDPATH+path).exists();
}
public File writeToSDFromInputStream(String path,String fileName,InputStream in){
File file=null;
OutputStream out = null;
createDirs(path);
try {
path=path+"/"+fileName;
System.out.println(path);
file=createFile(path);
out=new FileOutputStream(file);
byte[] buffer=new byte[4*1024];
while(in.read(buffer)!=-1){
out.write(buffer);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(out!=null)
out.close();
if(in!=null)
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
/*delete file
* @return 0 1
* */
public int deleteFile(String fileAllName){
File file=new File(SDPATH+fileAllName);
if(isFileExist(file.getPath())){
return 0;
}
file.delete();
return 1;
}
}
次はダウンロード
package duohuo.teng.net;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class HttpDownloadUtil {
/* String
* @param urlStr ip localhost 127.0.0.1, android IP, 10.0.2.2
* */
public String downloadString(String urlStr){
StringBuffer result=new StringBuffer();;
URL url;
InputStream in = null;
BufferedReader reader=null;
try {
url=new URL(urlStr);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
in=con.getInputStream();
reader=new BufferedReader(new InputStreamReader(in));
String line;
while((line=reader.readLine())!=null){
result.append(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(reader!=null)
reader.close();
if(in!=null)
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result.toString();
}
/* SD
* @return -1 0 1
* @param urlStr ip localhost 127.0.0.1, android IP, 10.0.2.2
* */
public int downloadFile(String urlStr,String toPath,String fileName){
try {
URL url=new URL(urlStr);
URLConnection con=url.openConnection();
InputStream in=con.getInputStream();
FileUtil fileUtil=new FileUtil();
if(fileUtil.isFileExist(toPath+fileName)){
return 0;
}
File file=fileUtil.writeToSDFromInputStream(toPath, fileName, in);
if(file==null){
return -1;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
return 1;
}
}