zoukankan      html  css  js  c++  java
  • 使用WebClient进行上传文件 [ZT]

       private bool UploadFile(string source,string targetUrl,NetworkCredential networkCredential)
            
    {
                FileStream streamSource;
                Stream streamTarget;
                WebClient client 
    = new WebClient();
                client.Credentials 
    = networkCredential;
                
    try
                
    {
                    streamSource 
    = File.OpenRead(source);
                }

                
    catch (Exception err)
                
    {
                    
    return false;
                }

                
    try
                
    {
                    Uri url 
    = new Uri(targetUrl);
                    streamTarget 
    = client.OpenWrite(url, "PUT");
                }

                
    catch (Exception err)
                
    {
                    
    return false;
                }

                
    try
                
    {
                    
    long num = 0;
                    
    int count = 0;
                    
    byte[] buffer = new byte[512];
                    
    while (num < streamSource.Length)
                    
    {
                        count 
    = streamSource.Read(buffer, 0512);
                        streamTarget.Write(buffer, 
    0, count);
                        num 
    += count;
                        
                    }

                    streamTarget.Close();
                    streamSource.Close();
                    
    return true;
                }

                
    catch (Exception err)
                
    {
                    
    return false;
                }

            }
     
  • 相关阅读:
    Java8实战之Stream流式操作
    类、对象、引用
    java对象的四种引用:强引用、软引用、弱引用和虚引用
    《将博客搬至CSDN》
    **JAVA参数传递方式 (按值传递与引用传递区别)
    JavaWeb--ServletContext
    HttpSessionListener的用法
    SpringMVC优雅的获取HttpSevletRequest及HttpServletResponse简录
    setnx
    JVM命令工具开发
  • 原文地址:https://www.cnblogs.com/RobotTech/p/928856.html
Copyright © 2011-2022 走看看