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.建立与教材配套的目录结构 ...
随机推荐
- D3.js学习记录
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 用JS获取地址栏参数的方法
采用正则表达式获取地址栏参数: function GetQueryString(name) { var reg = new RegExp("(^|&)"+ nam ...
- Ubuntu16.04 安装MATALAB R2015b教程
1.安装 将镜像文件内文件解压出来,添加执行权限,否则执行 ./install指令会出错 chmod -R 777 MATALAB 执行如下指令 ./install 2.填入补丁内的密匙 在Matla ...
- Mac键盘图标与对应快捷按键标志汇总
Mac键盘图标与对应快捷按键 ⌘--Command () win键 ⌃ --Control ctrl键 ⌥--Option (alt) ⇧--Shift ⇪--Caps Lock fn--功能键就是 ...
- C# Bitmap deep copy
今天在研究一个关于 Bitmap deep copy 的问题, 经过一系列的查询,在StackOverFlow上面找到了答案,遂记录下来: public static Bitmap DeepCopyB ...
- How to install OpenBazaar Server in CentOS7
helps from: https://github.com/OpenBazaar/OpenBazaar-Server http://stackoverflow.com/questions/24917 ...
- orm 语法 数据库连接、建表、增删改查、回滚、单键关联 、多键关联、三表关联
1.数据库连接, #!usr/bin/env/python # -*- coding:utf-8 -*- # from wangteng import sqlalchemy from sqlalche ...
- SQL的ROW_NUMBER函数
with tabs as ( select ROW_NUMBER() over(partition by customerID order by totalPrice) as rows,custome ...
- MySQL批量UPDATE多行记录
UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 ...
- Core Data使用
注意:每次修改CoreData的Attribute时记得把应用给删除重装,要不会崩,因为建立的数据库文件还在该目录下,里面的字段没有更改,所以不能匹配就会崩溃,切忌,要不就每次进来把文件先删除,再建立 ...