Created
September 14, 2012 11:05
-
-
Save Matsushige/3721325 to your computer and use it in GitHub Desktop.
CSVファイルダウンロード<サービス>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import org.apache.http.HttpException; | |
import android.app.IntentService; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.util.Log; | |
public class DownloadService extends IntentService{ | |
static final String TAG = "Download1"; | |
public DownloadService(){ | |
super(TAG); | |
}// DownloadService | |
@Override | |
protected void onHandleIntent(Intent intent){ | |
try{ | |
Bundle bundle = intent.getExtras(); | |
if(bundle == null){ | |
Log.d(TAG, "bundle == null"); | |
return; | |
}// if | |
String urlString = bundle.getString("url"); | |
URL url = new URL(urlString); | |
String fileName = getFileNameFromURL(url); | |
Log.d(TAG, fileName); | |
URLConnection conn = url.openConnection(); | |
HttpURLConnection httpConn = (HttpURLConnection) conn; | |
httpConn.setAllowUserInteraction(false); | |
httpConn.setInstanceFollowRedirects(true); | |
httpConn.setRequestMethod("GET"); | |
httpConn.connect(); | |
int response = httpConn.getResponseCode(); | |
if(response != HttpURLConnection.HTTP_OK){ | |
throw new HttpException(); | |
}// if | |
InputStream in = httpConn.getInputStream(); | |
FileOutputStream outStream = openFileOutput(fileName, MODE_PRIVATE); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); | |
String line; | |
OutputStreamWriter outWri = new OutputStreamWriter(outStream); | |
while((line = reader.readLine()) != null){ | |
outWri.write(line + "\n"); | |
}// while | |
sendBroadcast(fileName); | |
in.close(); | |
outWri.close(); | |
}catch(IOException e){ | |
Log.d(TAG, "IOException"); | |
}catch(HttpException e){ | |
Log.d(TAG, "HttpException"); | |
}// catch | |
}// onHandleIntent | |
protected void sendBroadcast(String fileName){ | |
Intent broadcastIntent = new Intent(); | |
Log.d(TAG, "fileName = " + fileName); | |
broadcastIntent.putExtra("fileName", fileName); | |
broadcastIntent.setAction("DOWNLOAD_ACTION"); | |
getBaseContext().sendBroadcast(broadcastIntent); | |
}// sendProgressBroadcast | |
protected String getFileNameFromURL(URL url){ | |
String[] p = url.getFile().split("/"); | |
String s = p[p.length - 1]; | |
if(s.indexOf("?") > -1){ | |
return s.substring(0, s.indexOf("?")); | |
}// if | |
return s; | |
}// getFileNameFromURL | |
}//DownloadService |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment