/**
* @param mContext 上下文
* @param targetUrl 文件上传地址
* @param filePath 文件路径
*/
public void uploadFile(final Activity mContext, String targetUrl, final String filePath) {
System.out.println("targetUrl: " + targetUrl + " filePath: " + filePath);
if (TextUtils.isEmpty(filePath)) {
Toast.makeText(mContext, "文件不存在", Toast.LENGTH_SHORT).show();
return;
}
final PostMethod filePost = new PostMethod(targetUrl) {//这个用来中文乱码
public String getRequestCharSet() {
return "UTF-8";
}
};
try {
final HttpClient client = new HttpClient();
File file = new File(filePath);
if (file.exists() && file.isFile()) {
long fileSize = file.length();
if (fileSize >= 5 * 1024 * 1024) {
Toast.makeText(mContext, "文件不得大于5M", Toast.LENGTH_SHORT).show();
return;
}
} else {
Toast.makeText(mContext, "文件不存在", Toast.LENGTH_SHORT).show();
return;
}
// 上传文件和参数
Part[] parts = new Part[]{new CustomFilePart(file.getName(), file),
new StringPart("filename", file.getName(), "UTF-8")};
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
new Thread(new Runnable() {
@Override
public void run() {
int statuscode = 0;
try {
statuscode = client.executeMethod(filePost);
} catch (IOException e) {
e.printStackTrace();
}
final int finalStatuscode = statuscode;
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
if (finalStatuscode == HttpStatus.SC_OK) {
Toast.makeText(mContext, "上传成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mContext, "上传失败", Toast.LENGTH_SHORT).show();
}
}
});
}
}).start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
final PostMethod filePost = new PostMethod(targetUrl) {//这个用来中文乱码
public String getRequestCharSet() {
return "UTF-8";
}
};
/**
* 解决中文文件名乱码
*/
public class CustomFilePart extends FilePart {
public CustomFilePart(String filename, File file)
throws FileNotFoundException {
super(filename, file);
}
protected void sendDispositionHeader(OutputStream out) throws IOException {
super.sendDispositionHeader(out);
String filename = getSource().getFileName();
if (filename != null) {
out.write(EncodingUtil.getAsciiBytes(FILE_NAME));
out.write(QUOTE_BYTES);
out.write(EncodingUtil.getBytes(filename, "UTF-8"));
out.write(QUOTE_BYTES);
}
}
}
Part[] parts = new Part[]{new CustomFilePart(file.getName(), file),
new StringPart("filename", file.getName(), "UTF-8")};
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
/**
* @param urlStr 文件地址
* @param path 文件保存路径
* @param fileName 文件名
* @return 文件的绝对路径
*/
public String downFile(String urlStr, String path, String fileName) {
InputStream inputStream = null;
String filePath = null;
try {
FileUtils fileUtils = new FileUtils();
//判断文件是否存在
if (fileUtils.isFileExist(path + fileName)) {
System.out.println("exits");
filePath = SDPATH + path + fileName;
} else {
//得到io流
inputStream = getInputStreamFromURL(urlStr);
//从input流中将文件写入SD卡中
File resultFile = fileUtils.write2SDFromInput(path, fileName, inputStream);
if (resultFile != null) {
filePath = resultFile.getPath();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return filePath;
}
/**
* 根据URL得到输入流
*
* @param urlStr
* @return
*/
public InputStream getInputStreamFromURL(String urlStr) {
HttpURLConnection urlConn;
InputStream inputStream = null;
try {
url = new URL(urlStr);
urlConn = (HttpURLConnection) url.openConnection();
inputStream = urlConn.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
return inputStream;
}
/**
* 将一个InputStream里面的数据写入到SD卡中
*
* @param path 文件保存路径
* @param fileName 文件保存的名字
* @param input 文件输入流
* @return 文件
*/
public File write2SDFromInput(String path, String fileName,
InputStream input) {
File file = null;
OutputStream output = null;
try {
// 创建文件夹
createSDDir(path);
// 创建文件
file = createSDFile(path + fileName);
// 开启输出流,准备写入文件
output = new FileOutputStream(file);
// 缓冲区
byte[] buffer = new byte[FILESIZE];
int count;
while ((count = input.read(buffer)) != -1) {
// 这里,请一定按该方式写入文件,不然时而会出现文件写入错误,数据丢失问题
output.write(buffer, 0, count);
}
output.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
while ((count = input.read(buffer)) != -1) {
// 这里,请一定按该方式写入文件,不然时而会出现文件写入错误,数据丢失问题
output.write(buffer, 0, count);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有