package com.streambus.setting.remoteserver;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.MethodNotSupportedException;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import com.streambus.setting.HXSettingApp;
import com.streambus.setting.util.Util;
import android.os.Environment;
import android.util.Log;
public class FileHelper implements HttpRequestHandler {
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
throws HttpException, IOException {
String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
if(!"GET".equals(method) && !"HEAD".equals(method) && !"POST".equals(method)) {
throw new MethodNotSupportedException("this method name " + method + "not supported");
}
if(request instanceof HttpEntityEnclosingRequest) {
FileItemFactory fileItemFactory=new DefaultFileItemFactory();
FileUpload fileUpload=new FileUpload(fileItemFactory);
try {
List<FileItem> items=fileUpload.parseRequest((HttpEntityEnclosingRequest)request);
Iterator<FileItem> it=items.iterator();
while(it.hasNext()){
FileItem item=it.next();
Log.d("test", " name "+item.getName());
Log.d("test", " size "+item.getSize());
if(item.isFormField()){
}else{
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//提示用户存储空间不足,不能进行上传
}else{
try {
File root=Environment.getExternalStorageDirectory();
InputStream in = item.getInputStream();
File file=new File(root.getAbsolutePath()+"/Remote_"+item.getName());
if(file.exists()){
//提示用户文件已存在,是否删除或者重新上传
file.delete();
}
FileOutputStream fos=new FileOutputStream(file);
int len=0;
byte[] buf=new byte[1024];
while((len=in.read(buf)) !=-1){
fos.write(buf, 0, len);
}
fos.close();
in.close();
Util.install(HXSettingApp.getContext(), file.getAbsolutePath());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//文件上传过程中发生异常
}
}
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 解码
*
* @param uri
* @return
*/
private String sanitizeUri(String uri) {
try {
uri = URLDecoder.decode(uri, "UTF-8");
} catch (UnsupportedEncodingException e) {
try {
uri = URLDecoder.decode(uri, "ISO-8859-1");
} catch (UnsupportedEncodingException e1) {
throw new Error();
}
}
return uri;
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.MethodNotSupportedException;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import com.streambus.setting.HXSettingApp;
import com.streambus.setting.util.Util;
import android.os.Environment;
import android.util.Log;
public class FileHelper implements HttpRequestHandler {
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
throws HttpException, IOException {
String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
if(!"GET".equals(method) && !"HEAD".equals(method) && !"POST".equals(method)) {
throw new MethodNotSupportedException("this method name " + method + "not supported");
}
if(request instanceof HttpEntityEnclosingRequest) {
FileItemFactory fileItemFactory=new DefaultFileItemFactory();
FileUpload fileUpload=new FileUpload(fileItemFactory);
try {
List<FileItem> items=fileUpload.parseRequest((HttpEntityEnclosingRequest)request);
Iterator<FileItem> it=items.iterator();
while(it.hasNext()){
FileItem item=it.next();
Log.d("test", " name "+item.getName());
Log.d("test", " size "+item.getSize());
if(item.isFormField()){
}else{
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//提示用户存储空间不足,不能进行上传
}else{
try {
File root=Environment.getExternalStorageDirectory();
InputStream in = item.getInputStream();
File file=new File(root.getAbsolutePath()+"/Remote_"+item.getName());
if(file.exists()){
//提示用户文件已存在,是否删除或者重新上传
file.delete();
}
FileOutputStream fos=new FileOutputStream(file);
int len=0;
byte[] buf=new byte[1024];
while((len=in.read(buf)) !=-1){
fos.write(buf, 0, len);
}
fos.close();
in.close();
Util.install(HXSettingApp.getContext(), file.getAbsolutePath());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//文件上传过程中发生异常
}
}
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 解码
*
* @param uri
* @return
*/
private String sanitizeUri(String uri) {
try {
uri = URLDecoder.decode(uri, "UTF-8");
} catch (UnsupportedEncodingException e) {
try {
uri = URLDecoder.decode(uri, "ISO-8859-1");
} catch (UnsupportedEncodingException e1) {
throw new Error();
}
}
return uri;
}
}