java上传文件

1、项目结构

在这里插入图片描述

2、demo

package up_file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;

public class up_file {
	// 共享文件夹所在服务器ip
    private static String USER_DOMAIN = "172.16.3.xxx";
    //访问用户
    private static String USER_ACCOUNT = "wwx";
    //访问密码
    private static String USER_PWS = "xxxxxx";
    //共享文件夹地址
    private static final String shareDirectory = "http://172.16.3.xxx/temp/123"; 
    //字节长度
    private static final int byteLen = 1024*10;
    

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File localFile=new File("D:\\test测试.txt");
		smbPut(shareDirectory, localFile);
	}
	
	
	
	public static void smbPut(String shareDirectory, File localFile) {
        InputStream in = null;
        OutputStream out = null;
        try {
            String fileName = localFile.getName();     
            // 域服务器验证
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(USER_DOMAIN, USER_ACCOUNT,USER_PWS);
            SmbFile remoteFile = new SmbFile(shareDirectory + File.separator + fileName, auth);  
            
//            解决办法: SmbFile remoteFile = new SmbFile(remotePhotoUrl+File.separator+fileName,auth);
//            改为:SmbFile remoteFile = new SmbFile(remotePhotoUrl+"/"+fileName,auth);便可以进行文件上传
            
            in = new BufferedInputStream(new FileInputStream(localFile));
            out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
            byte[] buffer = new byte[byteLen];
            while (in.read(buffer) != -1) {
                out.write(buffer);
                buffer = new byte[byteLen];
            }
            out.flush();
            System.out.println("上传成功!");            
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("失败1");
        } finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("失败2");
            }
        }
    }
	
	

}

3、运行结果

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值