java文件下载,上传,解压方法
1、文件下载(亲测可用)
private static final int BUFFER = 2 * 1024;// 缓冲区大小(2k)private boolean isSuccess = true;//成功标志
public void downFile(String urlStr, String path, String fileName) {
OutputStream output = null;
try {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 取得inputStream,并将流中的信息写入
String pathName = path + fileName;// 文件存储路径
File file = new File(pathName);
InputStream input = conn.getInputStream();
if (file.exists()) {
return;
} else {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
file.createNewFile();// 新建文件
output = new FileOutputStream(file);
int count;
byte[] buffer = new byte[2 * 1024];
while ((count = input.read(buffer)) != -1) {
output.write(buffer, 0, count);
}
output.flush();
}
output.close();
} catch (Exception e) {
e.printStackTrace();
isSuccess = false;
}
}
2、文件上传
public String uploadFile(String uploadUrl, String uploadFile, String newName) {
String LINE_END = "\r\n"; //数据结束标志
String PREFIX = "--"; //数据前缀
String BOUNDARY = "*****"; //边界标识
String resultStr = ""; try {
URL url = new URL(uploadUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// 允许Input、Output,不使用Cache
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
// 设置传送的method=POST
con.setRequestMethod("POST"); con.setConnectTimeout(120 * 1000);// (单位:毫秒) con.setReadTimeout(60 * 1000);// (单位:毫秒)
// 设置请求属性
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + BOUNDARY); con.connect();
// 文件输出流
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
ds.writeBytes(PREFIX + BOUNDARY + LINE_END);
ds.writeBytes("Content-Disposition: form-data; "
+ "name=\"file1\";filename=\"" + newName + "\"" + LINE_END);
ds.writeBytes(LINE_END);
// 取得文件的FileInputStream
FileInputStream fStream = new FileInputStream(uploadFile);
byte[] buffer = new byte[BUFFER];
int length = -1;
while ((length = fStream.read(buffer)) != -1) {
ds.write(buffer, 0, length);
}
ds.writeBytes(LINE_END);
ds.writeBytes(PREFIX + BOUNDARY + PREFIX + LINE_END);
fStream.close();
ds.flush();
// 取得Response内容
int resCode = con.getResponseCode();
if (resCode == 200) {
InputStream is = con.getInputStream();
int ch;
StringBuffer result = new StringBuffer();
while ((ch = is.read()) != -1) {
result.append((char) ch);
} resultStr = result.toString();
}
ds.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return resultStr ;
}
3、文件解压(亲测可用)
public void UnZipFolder(String zipFileString, String outPathString) {
File desDir = new File(outPathString);
if (!desDir.exists()) {
desDir.mkdirs();
}
try {
@SuppressWarnings("resource")
ZipFile zf = new ZipFile(zipFileString);//根据文件创建ZipFile
//遍历压缩文件条目
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
//获取条目的输入流
InputStream in = zf.getInputStream(entry);
String str = outPathString + entry.getName();//条目路径
str = new String(str.getBytes("8859_1"), "GB2312");
File desFile = new File(str);
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
desFile.createNewFile();//创建新文件
}
OutputStream out = new FileOutputStream(desFile);
byte buffer[] = new byte[2 * 1024];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
isSuccess = false;
}
}
java文件下载,上传,解压方法的更多相关文章
- Java 上传解压zip文件,并且解析文件里面的excel和图片
需求:上传一个zip文件,zip文件里面包含一个excel和很多图片,需要把excel里面的信息解析出来保存到表中,同时图片也转化成base64保存到数据库表中. PS:为了方便不同水平的开发人员阅读 ...
- mac通过自带的ssh连接Linux服务器并上传解压文件
需求: 1:mac连接linux服务器 2:将mac上的文件上传到linux服务器指定位置 3:解压文件 mac上使用命令,推荐使用 iterm2 .当然,也可以使用mac自带的终端工具. 操作过程: ...
- java uploadify 上传组件使用方法
!!!声明 1-3 是jsp页面所写内容 文中需要的util 参见百度云 http://pan.baidu.com/s/1kV0gqBt 如已失效 请加QQ1940978083 1.首先引入 ...
- java压缩包上传,解压,预览(利用editor.md和Jstree实现)和下载
java压缩包上传,解压,预览(利用editor.md和Jstree实现)和下载 实现功能:zip文件上传,后台自动解压,Jstree树目录(遍历文件),editor.md预览 采用Spring+Sp ...
- Java ftp上传文件方法效率对比
Java ftp上传文件方法效率对比 一.功能简介: txt文件采用ftp方式从windows传输到Linux系统: 二.ftp实现方法 (1)方法一:采用二进制流传输,设置缓冲区,速度快,50M的t ...
- 2013第38周日Java文件上传下载收集思考
2013第38周日Java文件上传&下载收集思考 感觉文件上传及下载操作很常用,之前简单搜集过一些东西,没有及时学习总结,现在基本没啥印象了,今天就再次学习下,记录下自己目前知识背景下对该类问 ...
- Myeclipse10.7安装git插件并将Java项目上传到码云(github)
注:本文来源:外匹夫的<Myeclipse10.7安装git插件并将Java项目上传到码云(github)> 一.先说说安装egit插件的步骤(安装egit不成功的原因主要是下载的egit ...
- 小兔Java教程 - 三分钟学会Java文件上传
今天群里正好有人问起了Java文件上传的事情,本来这是Java里面的知识点,而我目前最主要的精力还是放在了JS的部分.不过反正也不麻烦,我就专门开一贴来聊聊Java文件上传的基本实现方法吧. 话不多说 ...
- linux下各种解压方法
linux下各种格式的压缩包的压缩.解压方法.但是部分方法我没有用到,也就不全,希望大家帮我补充,我将随时修改完善,谢谢! .tar 解包:tar xvf FileName.tar 打包:t ...
- 将linux上的Java代码上传到码云
将linux上的Java代码上传到码云 1.在linux上直接输入命令获取git sudo apt-get install git 显示资源被占用,按照图中方法强制安装 2.建立与教材配套的目录结构 ...
随机推荐
- 高效的jQuery
选择捷径 // 糟糕 if(collection.length > 0){..} // 建议 if(collection.length){..} 熟记技巧 // 糟糕 $('#id').data ...
- hive单机安装(实战)
hive使用与注意事项:http://blog.csdn.net/stark_summer/article/details/44222089 连接命令:beeline -n root -u jdbc: ...
- CSS修改input[type=range]滑块样式
input[type="range"]是html5中的input标签新属性,样子如下: <input type="range" value="4 ...
- [Asp.net MVC]Asp.net MVC5系列——在模型中添加验证规则
目录 概述 在模型中添加验证规则 自定义验证规则 伙伴类的使用 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5 ...
- Android 全局获取 Context 与使用 Intent 传递对象
=====================全局获取 Context======================== Android 开发中很多地方需要用到 Context,比如弹出 Toast.启动活 ...
- .deb包的安装方法
deb是Debian linux的安装格式,跟redhat的rpm非常相似,最基本的安装命令是: dpkg -i file.deb dpkg是Debian Package的简写,是为Debian专门开 ...
- DB2 SQL 日期函数
DB2 SQL 日期函数1:CURRENT TIMESTAMP 函数:获取当前日期时间语法:CURRENT TIMESTAMP参数:当前日期时间返回值:当前日期时间 2:CURRENT DATE 函数 ...
- HTML中图像代替提交按钮
1. 用图像代替提交按钮 当只有一个提交按钮的时候 ,可以简单的实现,不用添加事件函数,代码是: <input type = "image"' name = ".. ...
- ios build时,Undefined symbols for architecture xxx问题的总结
简单来说,Undefined symbols基本上等于JAVA的ClassNotFoundException,最常见的原因有这几种: build的时候没有加framework 比如说,有一段代码我用了 ...
- hive数据操作
mdl是数据操作类的语言,包括向数据表加载文件,写查询结果等操作 hive有四种导入数据的方式 >从本地加载数据 LOAD DATA LOCAL INPATH './examples/files ...