C# 下载PDF文件(http与ftp)
1.下载http模式的pdf文件(以ASP.NET为例,将PDF存在项目的目录下,可以通过http直接打开项目下的pdf文件)
#region 调用本地文件使用返回pdfbyte数组
/// <summary>
/// 调用本地文件使用返回pdfbyte数组
/// </summary>
/// <param name="srcPdfFile">‘D:\in2434341555551.pdf’</param>
/// <returns></returns>
public static byte[] GetSignaturePDFByte(string srcPdfFile)
{
using (FileStream fsRead = new FileStream(srcPdfFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
int fsLen = (int)fsRead.Length;
byte[] hebyte = new byte[fsLen];
fsRead.Read(hebyte, , hebyte.Length);
return hebyte;
}
}
#endregion 调用本地文件使用返回pdfbyte数组
#region 从网站上下载pdf,转化为字节流
/// <summary>
/// 从网站上下载pdf,转化为字节流
/// </summary>
/// <param name="srcPdfFile">文件地址:'https://******/group2/M00/00/04/wKj-mlpcoZ2IUbK5AACrpaV6k98AAAB6gAAAAAAAKu9562.pdf'</param>
/// <returns></returns>
public static Byte[] GetByteByRemoteURL(string srcPdfFile)
{
byte[] arraryByte;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(srcPdfFile);
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
StreamReader responseStream = new StreamReader(wr.GetResponseStream(), Encoding.UTF8);
int length = (int)wr.ContentLength;
byte[] bs = new byte[length];
HttpWebResponse response = wr as HttpWebResponse;
Stream stream = response.GetResponseStream();
//读取到内存
MemoryStream stmMemory = new MemoryStream();
byte[] buffer1 = new byte[length];
int i;
//将字节逐个放入到Byte 中
while ((i = stream.Read(buffer1, , buffer1.Length)) > )
{
stmMemory.Write(buffer1, , i);
}
arraryByte = stmMemory.ToArray();
stmMemory.Close();
}
return arraryByte;
}
#endregion 从网站上下载pdf,转化为字节流
#region 从网站上下载文件,保存到其他路径
/// <summary>
/// 从网站上下载文件,保存到其他路径
/// </summary>
/// <param name="pdfFile">文件地址</param>
/// <param name="saveLoadFile">保存文件路径:D:\12221.pdf</param>
/// <returns></returns>
public string SaveRemoteFile( string saveLoadFile , string pdfFile)
{
//bool flag = false;
var f = saveLoadFile + Guid.NewGuid().ToString("D") + ".pdf";
Uri downUri = new Uri(pdfFile);
//建立一个WEB请求,返回HttpWebRequest对象
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(downUri);
//流对象使用完后自动关闭
using (Stream stream = hwr.GetResponse().GetResponseStream())
{
//文件流,流信息读到文件流中,读完关闭
using (FileStream fs = File.Create(f))
{
//建立字节组,并设置它的大小是多少字节
byte[] bytes = new byte[];
int n = ;
while (n > )
{
//一次从流中读多少字节,并把值赋给N,当读完后,N为0,并退出循环
n = stream.Read(bytes, , );
fs.Write(bytes, , n); //将指定字节的流信息写入文件流中
}
}
}
//return flag;
//return _outPath + saveLoadFile;
return f;
}
#endregion 从网站上下载文件,保存到其他路径
2.ftp模式的pdf文件
/// <summary>
/// 下载FTP文件。
/// </summary>
/// <param name="offsetPath">相对路径</param>
/// <param name="fileName">文件名称</param>
/// <returns>下载结果,本地文件路径</returns>
public string DownLoad(string offsetPath,string fileName)
{
try
{
FtpWebRequest ftpWeb = (FtpWebRequest)WebRequest.Create(_ftpRootPath + offsetPath + fileName);
ftpWeb.Method = WebRequestMethods.Ftp.DownloadFile;
ftpWeb.UseBinary = true;
var resp = ftpWeb.GetResponse();
using (FileStream fs = new FileStream(_outPath + fileName, FileMode.Create))
{
using (var s = resp.GetResponseStream())
{
if (s == null) { return "文件不存在!"; } int readCout = ;
byte[] bytes = new byte[];
readCout = s.Read(bytes, , );
while (readCout > )
{
fs.Write(bytes, , readCout);
readCout = s.Read(bytes, , );
}
}
}
resp.Close();
return _outPath + fileName;
}
catch (Exception e)
{
return e.Message;
} } /// <summary>
/// 判断文件是否存在
/// </summary>
/// <param name="offsetPath"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public bool FileExists(string offsetPath, string fileName)
{
try
{
FtpWebRequest ftpWeb = (FtpWebRequest)WebRequest.Create(_ftpRootPath + offsetPath + fileName);
ftpWeb.Method = WebRequestMethods.Ftp.DownloadFile;
ftpWeb.UseBinary = true;
var resp = (FtpWebResponse)ftpWeb.GetResponse();
resp.Close();
return true;
}
catch (Exception)
{
return false;
}
} /// <summary>
/// 获取目录下所有文件
/// </summary>
/// <returns></returns>
public string[] Files(string offsetPath)
{
try
{
FtpWebRequest ftpWeb = (FtpWebRequest)WebRequest.Create(_ftpRootPath + offsetPath);
ftpWeb.Method = WebRequestMethods.Ftp.ListDirectory;
Stream stream = ftpWeb.GetResponse().GetResponseStream();
if (stream == null)
{
return null;
}
List<string> fileList = new List<string>();
using (StreamReader sr = new StreamReader(stream))
{
StringBuilder sb = new StringBuilder();
do
{
sb.Append(sr.ReadLine());
if (sb.Length > )
{
fileList.Add(sb.ToString());
sb.Clear();
}
else
{
break;
}
} while (true);
}
return fileList.ToArray();
}
catch (Exception)
{
return null;
}
}
C# 下载PDF文件(http与ftp)的更多相关文章
- 阿里云OSS下载pdf文件,并在pdf文件上添加水印
代码: 兵马未动,粮草先行 作者: 传说中的汽水枪 如有错误,请留言指正,欢迎一起探讨. 转载请注明出处. 公司要求从阿里云OSS下载pdf文件并且需要添加水印. 因此这里总结一下. 首先添加了一个F ...
- 使用java的 htpUrlConnection post请求 下载pdf文件,然后输出到页面进行预览和下载
使用java的 htpUrlConnection post请求 下载pdf文件,然后输出到页面进行预览和下载 2018年06月07日 10:42:26 守望dfdfdf 阅读数:235 标签: jav ...
- 知网下载pdf文件的方法
title: 知网下载pdf文件的方法 toc: false date: 2018-11-02 17:54:43 categories: methods tags: 知网 平时我们使用的是国内版的知网 ...
- ftp下载目录文件 不需要ftp脚本
ftp下载目录文件 不需要ftp脚本 wget ftp://192.168.1.37:21/checkpoints --ftp-user=ftpadmin --ftp-password=gaofeng ...
- 从七牛服务下载PDF文件
/** * 从七牛下载PDF文件 * @param request * @param response * @param exhiId * @throws MalformedURLException ...
- 【转】Python编程: 多个PDF文件合并以及网页上自动下载PDF文件
1. 多个PDF文件合并1.1 需求描述有时候,我们下载了多个PDF文件, 但希望能把它们合并成一个PDF文件.例如:你下载的数个PDF文件资料或者电子发票,你可以使用python程序合并成一个PDF ...
- 项目中的那些事---下载pdf文件
最近做了一个下载pdf文档的需求,本以为使用HTML5中<a>标签的属性download就能简单搞定,不料IE竟然不支持这一简单粗暴的H5新特性,而是直接在网页中打开, 于是各种搜索之后得 ...
- .NetMvc从http或本地下载pdf文件
1.帮助类 1 public static class PdfHelper 2 { 3 #region 从http链接下载 4 public static void Download(string u ...
- android默认浏览器response下载PDF文件
下载出来的文件不是PDF,而是xxx.htm文件,原因是response的header配置有问题. android默认浏览器的情况下,header的配置应该写成.(java 为例) response. ...
随机推荐
- [SDOI2015]寻宝游戏(LCA,set)
[SDOI2015]寻宝游戏 题目描述 小B最近正在玩一个寻宝游戏,这个游戏的地图中有N个村庄和N-1条道路,并且任何两个村庄之间有且仅有一条路径可达.游戏开始时,玩家可以任意选择一个村庄,瞬间转移到 ...
- python如何查看内存占用空间
我们如何查看变量占用了多少内存空间呢 首先我们引用sys模块,在使用getsizeof()方法 import sys L = [x for x in range(10000)] print(sys.g ...
- Python语言为什么被称为高级程序设计语言?
Python是一种令人惊叹的编程语言,毫无疑问.从1991年的卑微开始,它现在几乎无处不在.无论您是在进行Web开发,系统管理,测试自动化,devop还是数据科学,Python在您的工作中发挥作用的可 ...
- count(*),count(1),count(列名)的区别
count(*)和count(1)无任何差别,永远优于count其他字段只要存在普通索引,count就会使用普通索引,只存在主键时,count(*)和或count(1)会使用主键索引 count(a) ...
- java源码生成可运行jar
参考资料:https://blog.csdn.net/whatday/article/details/54767187 源码目录层级如下:
- 百度编辑器UEditor使用总结
官网下载地址:http://ueditor.baidu.com/website/download.html 我下载的是jsp版本,下载后将整个目录复制到项目的js包下,然后将jsp包下的lib下的ja ...
- CSS基础知识总结二
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/html"> ...
- ht-8 对arrayList中的自定义对象排序( Collections.sort(List<T> list, Comparator<? super T> c))
package com.iotek.set; import java.util.ArrayList; import java.util.Collections; import java.util.Co ...
- Windows无法启动MapGIS DataStorage Service服务
但是启动又启动不了,查看属性 发现计算机服务器确实少了该文件目录.. 可能是不小心删除了? 之前确实有删过一些文件 下次直接把.net禁止就可以了,不用删除,不然不小心删除了其它服务.. 参考文献:h ...
- Linux 系统下文件夹与文件的读写可执行权限问题
linux是一个多用户操作系统,linux对文件系统内的所有文件,实行了严格的权限划分管理.防止没有权限的用户访问某个文件.linux文件或目录的权限分为 读.写.可执行三种权限.文件访问的用户类别分 ...