Java实现上传和访问被拒绝问题解决

**
请大家多多支持多多关注 ,谢谢各位IT界的精英们!!!
请大家多多支持多多关注 ,谢谢各位IT界的精英们!!!
请大家多多支持多多关注 ,谢谢各位IT界的精英们!!!
***

创建SpringBoot工程:

https://blog.csdn.net/weixin_45423451/article/details/103137733

访问被拒绝问题

https://blog.csdn.net/weixin_45423451/article/details/103381215

Java实现上传小demo

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Description:上传小demo
 */
public class UpFile{

    public static void main(String[] args) throws InterruptedException {

        String a ="D:\\file\\新建文本文档.txt";
        File f =new File(a.trim());
		//方法1
		//copyOne(f);
		//方法2
       copyTwo(f);
        System.exit(0);
    }
	//方法1
   public static void copyOne(File f){

       FileInputStream in= null;
       BufferedInputStream buffIn= null;
       try {
           in =new FileInputStream(f);
           buffIn = new BufferedInputStream(in);
           String name = f.getName();
           Path path = Paths.get("D:\\"+ File.separator+name);
           byte file[] = new byte[buffIn.available()];
           int read = in.read(file);
           Files.write(path,file);
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }finally {
           close(in,buffIn,null);
       }
   }
	//方法2
    public static void copyTwo(File f){

        FileInputStream in= null;
        BufferedInputStream buffIn= null;
        FileOutputStream out= null;
        try {
            in =new FileInputStream(f);
            buffIn = new BufferedInputStream(in);
            out=new FileOutputStream("D:\\新建.txt");
            int i = buffIn.available();
            byte buffer[] = new byte[i];
            int len = 0;
            // 循环将输入流中的内容读取到缓冲区当中
            while ((len = buffIn.read(buffer)) > 0) {
                // 输出缓冲区的内容到浏览器,实现文件下载
                out.write(buffer, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            close(in,buffIn,out);
        }
    }
	//关闭流
    public static void close(FileInputStream in,BufferedInputStream buffIn,FileOutputStream out){
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        if(buffIn != null){
            try {
                buffIn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值