JAVA下载、删除、修改文件名、
2018-01-25 15:00
369 查看
JAVA文件的下载
/**
* 下载文件
*
* @param savepath 保存路径
* @param resurl 资源路径
* @param fileName 自定义资源名
*/
public String getInternetRes(String savepath, String resurl, String fileName) {
URL url = null;
HttpURLConnection con = null;
InputStream in = null;
FileOutputStream out = null;
File res = null;
try {
url = new URL(resurl);
//建立http连接,得到连接对象
con = (HttpURLConnection) url.openConnection();
in = con.getInputStream();
byte[] data = getByteData(in);//转化为byte数组
File file = new File(savepath);
if (!file.exists()) {
file.mkdirs();
}
res = new File(file + File.separator + fileName);
out = new FileOutputStream(res);
out.write(data);
logger.info("downloaded successfully!!!");
} catch (IOException e) {
logger.error(e.getMessage() , e);
} finally {
try {
if (null != out)
out.close();
if (null != in)
in.close();
} catch (IOException e) {
logger.error(e.getMessage() , e);
}
}
return res.toString();
}
/**
* 从输入流中获取字节数组
*
* @param in
* @return
* @throws IOException
*/
private byte[] getByteData(InputStream in) throws IOException {
byte[] b = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len = 0;
while ((len = in.read(b)) != -1) {
bos.write(b, 0, len);
}
if(null!=bos){
bos.close();
}
return bos.toByteArray();
}
2、修改文件名
/**
* 修改文件名字
* @param filePath 文件路径
* @param newName 新的文件名
* @return
*/
public boolean updateFileName(String filePath , String newName){
File f = new File(filePath);
String c = f.getParent();
File mm = new File(c + sb.toString());
if (f.renameTo(mm)) {
return true;
} else {
return false;
}
}3、删除文件
/**
* 删除文件
* @param filePath 文件路径
* @param format 文件后缀
*/
public void deleteFile(String filePath , String format){
StringBuffer sb = new StringBuffer(filePath);
sb.append(format);
File f = new File(sb.toString());
if(f.exists())
f.delete();
}
/**
* 下载文件
*
* @param savepath 保存路径
* @param resurl 资源路径
* @param fileName 自定义资源名
*/
public String getInternetRes(String savepath, String resurl, String fileName) {
URL url = null;
HttpURLConnection con = null;
InputStream in = null;
FileOutputStream out = null;
File res = null;
try {
url = new URL(resurl);
//建立http连接,得到连接对象
con = (HttpURLConnection) url.openConnection();
in = con.getInputStream();
byte[] data = getByteData(in);//转化为byte数组
File file = new File(savepath);
if (!file.exists()) {
file.mkdirs();
}
res = new File(file + File.separator + fileName);
out = new FileOutputStream(res);
out.write(data);
logger.info("downloaded successfully!!!");
} catch (IOException e) {
logger.error(e.getMessage() , e);
} finally {
try {
if (null != out)
out.close();
if (null != in)
in.close();
} catch (IOException e) {
logger.error(e.getMessage() , e);
}
}
return res.toString();
}
/**
* 从输入流中获取字节数组
*
* @param in
* @return
* @throws IOException
*/
private byte[] getByteData(InputStream in) throws IOException {
byte[] b = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len = 0;
while ((len = in.read(b)) != -1) {
bos.write(b, 0, len);
}
if(null!=bos){
bos.close();
}
return bos.toByteArray();
}
2、修改文件名
/**
* 修改文件名字
* @param filePath 文件路径
* @param newName 新的文件名
* @return
*/
public boolean updateFileName(String filePath , String newName){
File f = new File(filePath);
String c = f.getParent();
File mm = new File(c + sb.toString());
if (f.renameTo(mm)) {
return true;
} else {
return false;
}
}3、删除文件
/**
* 删除文件
* @param filePath 文件路径
* @param format 文件后缀
*/
public void deleteFile(String filePath , String format){
StringBuffer sb = new StringBuffer(filePath);
sb.append(format);
File f = new File(sb.toString());
if(f.exists())
f.delete();
}
相关文章推荐
- java基于Apache FTP实现文件上传、下载、修改文件名、删除
- Java通过URL下载网络文件,获取文件流并修改文件名
- Apache FTP文件上传、下载、修改文件名、删除
- Apache FTP多线程文件上传、下载、修改文件名、删除
- Java利用SMB上传、下载、读取(内容或文件名)、复制、删除远程文件
- Java修改文件夹下所有文件名
- java下载文件文件名针对不同浏览器乱码,文件名中存在括号等乱码问题
- java下载中文文件名乱码的解决方法
- [JavaWeb基础] 004.用JSP + SERVLET 进行简单的增加删除修改
- java使用jdbc对sqlite 添加、删除、修改的操作
- Java如何大批量从json数据源中按指定符号隔字符串,并修改、删除数据
- java的小说展示,下载在查看所有(文件路径找不到,可以修改)
- java实现读取文件底下所有子文件名,并修改文件名
- java实现对hdfs文件系统的上传,下载,删除,创建文件夹的操作演示
- JSP实现文件上传下载和删除(附:JS获取上传文件的文件名)
- java 操作mongodb插入、读取、修改以及删除基础
- Java day10 IO 列出文件下的所有层级文件名 待修改
- java 修改删除返回当前页面问题解决办法
- java for-each本质--不能增加和删除,但是可以修改当前元素
- java文件下载程序中,解决汉字文件名乱码