/**
* <p>project_name:contacts</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2011 by tl3shi.</p>
**/
package com.qiaozi.luying;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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.URL;
import java.util.Map;
import android.os.Environment;
import android.util.Log;
/**
* <p>Title: FileUtil.java</p>
* <p>Description: </p>
* @author <a href="mailto:[email protected]">Administrator</a>
* @date 2011-3-3 下午03:28:05
* @version 1.0
*/
public class FileUtil
{
private String SDPATH;
private int FILESIZE = 4*1024;
public String getSDPATH(){
return SDPATH;
}
public FileUtil(){
//得到当前外部存储设备的目录( /SDCARD )
SDPATH = Environment.getExternalStorageDirectory() + "/";
}
/**
* 在SD卡上创建文件
* @param fileName
* @return
* @throws IOException
*/
public File createSDFile(String fileName) throws IOException{
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}
/**
* 在SD卡上创建目录
* @param dirName
* @return
*/
public File createSDDir(String dirName){
File dir = new File(SDPATH + dirName);
dir.mkdir();
return dir;
}
/**
* 判断SD卡上的文件夹是否存在
* @param fileName
* @return
*/
public boolean isFileExist(String fileName){
File file = new File(SDPATH + fileName);
return file.exists();
}
/**
* 将一个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];
while((input.read(buffer)) != -1){
output.write(buffer);
}
output.flush();
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try {
if(null != output){
output.close();
}
if(null != input){
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
/**
* 直接通过HTTP协议提交数据到服务器,实现表单提交功能
* @param actionUrl 上传路径
* @param params 请求参数 key为参数名,value为参数值
* @param file 上传文件
*/
public static String post(String actionUrl, Map<String, String> params, FormFile[] files) {
try {
String BOUNDARY = "---------7d4a6d158c9"; //数据分隔线
String MULTIPART_FORM_DATA = "multipart/form-data";
URL url = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(6000);
conn.setDoInput(true);//允许输入
conn.setDoOutput(true);//允许输出
conn.setUseCaches(false);//不使用Cache
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);
StringBuilder sb = new StringBuilder();
//上传的表单参数部分,格式请参考文章
for (Map.Entry<String, String> entry : params.entrySet()) {//构建表单字段内容
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\""+ entry.getKey() + "\"\r\n\r\n");
sb.append(entry.getValue());
sb.append("\r\n");
}
DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
outStream.write(sb.toString().getBytes());//发送表单字段数据
FileInputStream fis = null;
//上传的文件部分,格式请参考文章
for(FormFile file : files){
StringBuilder split = new StringBuilder();
split.append("--");
split.append(BOUNDARY);
split.append("\r\n");
split.append("Content-Disposition: form-data;name=\""+ file.getFormname()+"\";filename=\""+ file.getFilname() + "\"\r\n");
split.append("Content-Type: "+ file.getContentType()+"\r\n\r\n");
outStream.write(split.toString().getBytes());
outStream.write(file.getData(), 0, file.getData().length);
outStream.write("\r\n".getBytes());
}
byte[] end_data = ("--" + BOUNDARY + "--\r\n").getBytes();//数据结束标志
outStream.write(end_data);
outStream.flush();
int cah = conn.getResponseCode();
Log.i("userinfo", ""+cah);
if (cah != 200) throw new RuntimeException("请求url失败");
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");//解决乱码问题,保证服务器端返回数据正常显示
BufferedReader br = new BufferedReader(isr);
String result = br.readLine();
outStream.close();
conn.disconnect();
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 读取源文件内容
* @param filename String 文件路径
* @throws IOException
* @return byte[] 文件内容
*/
public static byte[] readFile(String filename) {
File file =new File(filename);
long len = file.length();
Log.i("userinfo", "file.length():"+len);
byte[] bytes = new byte[(int)len];
BufferedInputStream bufferedInputStream;
try
{
bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
int r = bufferedInputStream.read( bytes );
if (r != len){ bufferedInputStream.close();}
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return bytes;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
android客户端与web服务器端的文件上传与下载源代码,和android录音功能。 HttpDownloader.java:下载文件 FileUtil.java:post方法上传文件 MainActivity.java:录音功能 TestFileActivity.java:测试文件的上传与下载
资源推荐
资源详情
资源评论
收起资源包目录
javaweb+android(上传下载).rar (36个子文件)
LUYING
.project 842B
proguard.cfg 1KB
src
com
qiaozi
luying
TestFileActivity.java 2KB
HttpDownloader.java 1KB
MainActivity.java 2KB
FileUtil.java 6KB
FormFile.java 2KB
AndroidManifest.xml 1KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 3KB
layout
upload.xml 570B
main.xml 734B
values
strings.xml 169B
.settings
org.eclipse.core.resources.prefs 86B
assets
gen
com
qiaozi
luying
R.java 1016B
default.properties 362B
.classpath 280B
bin
resources.ap_ 11KB
LUYING.apk 20KB
com
qiaozi
luying
R$layout.class 421B
R$drawable.class 397B
TestFileActivity.class 2KB
R$attr.class 337B
FormFile.class 1KB
TestFileActivity$2.class 1KB
TestFileActivity$1.class 2KB
R$id.class 523B
FileUtil.class 7KB
MainActivity$2.class 1KB
R.class 524B
R$string.class 424B
MainActivity$1.class 3KB
HttpDownloader.class 2KB
MainActivity.class 1KB
classes.dex 14KB
共 36 条
- 1
xinxianzhongndsc
- 粉丝: 2
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 历史学习网站 JAVA+Vue.js+SpringBoot+MySQL
- µ×ɼµÂ͵ƻºÍÒ¿ÏÍɼÂÎ×Á
- 基于pytorch+OpenCV的手写数字识别源码+使用文档+全部资料(优秀项目).zip
- C++ 一个 回文素数 回文素 数
- C++ 一个 回文素数 回文素 数
- 基于pytorch+OpenCV的手写数字识别源码+使用文档+全部资料(优秀项目).zip
- C++ 一个 回文素数 回文素 数
- 基于ME4056+ME2109芯片移动电源硬件设计PDF原理图+PADS9.3 PCB文件.zip
- 基于Python+OpenCV的印章识别程序源码+使用文档+全部资料(优秀项目).zip
- python 写 一个 回文素数
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页