栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java根据路径下载文件

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java根据路径下载文件

package com.sinosoft.cms.common.util;

//An highlighted block

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.UnsupportedEncodingException;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

import java.net.URLDecoder;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Random;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

public class Caiji {

// public static void main(String[] args) throws UnsupportedEncodingException

// {

//

// String photoUrl = "http://XXXXXXXXXXXXXXX.pdf";

// //

//

// String fileName = photoUrl.substring(photoUrl.lastIndexOf("/")); //为下载的文件命名

// fileName = URLDecoder.decode(fileName,"utf-8");

// String filePath = "D:/caiji/"+fileName+"/"; //保存目录

//

// File file = saveUrlAs(photoUrl, filePath ,"GET");

// }

public static String getRandomStr() {

int length = 6;

String base = "abcdefghijklmnopqrstuvwxyz0123456789";

Random random = new Random();

StringBuffer sb = new StringBuffer();

for (int i = 0; i < length; i++) {

int number = random.nextInt(base.length());

sb.append(base.charAt(number));

}

return sb.toString();

}

public static File saveUrlAs(String url,String filePath,String method){

//System.out.println("fileName---->"+filePath);

//创建不同的文件夹目录

File file=new File(filePath);

//判断文件夹是否存在

if (!file.exists())

{

//如果文件夹不存在,则创建新的的文件夹

file.mkdirs();

}

FileOutputStream fileOut = null;

HttpURLConnection conn = null;

InputStream inputStream = null;

try

{

// 建立链接

URL httpUrl=new URL(url);

conn=(HttpURLConnection) httpUrl.openConnection();

//以Post方式提交表单,默认get方式

conn.setRequestMethod(method);

conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

conn.setDoInput(true);

conn.setDoOutput(true);

// post方式不能使用缓存

conn.setUseCaches(false);

//连接指定的资源

conn.connect();

//获取网络输入流

inputStream=conn.getInputStream();

BufferedInputStream bis = new BufferedInputStream(inputStream);

//判断文件的保存路径后面是否以/结尾

// if (!filePath.endsWith("/")) {

//

// filePath += "/";

//

// }

//写入到文件(注意文件保存路径的后面一定要加上文件的名称)

fileOut = new FileOutputStream(filePath+getRandomStr()+".pdf");

BufferedOutputStream bos = new BufferedOutputStream(fileOut);

byte[] buf = new byte[4096];

int length = bis.read(buf);

//保存文件

while(length != -1)

{

bos.write(buf, 0, length);

length = bis.read(buf);

}

bos.close();

bis.close();

conn.disconnect();

} catch (Exception e)

{

e.printStackTrace();

System.out.println("抛出异常!!");

}

return file;

}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023590.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号