springboot文件上传--单文件上传

2021-05-12 02:28

阅读:531

YPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

标签:ansi   type   byte   ast   文件上传   相对路径   pos   family   files   

1、前端页面

Insert title here

单文件上传:

2、后端上传方法,文件一共有三种上传路径

      1、使用相对路径,文件保存路径为D:/fxglxt/target/classes/static/web/uploadFiles

      2、使用绝对路径,文件上传到D:/fxglxt/src/main/resources/static/web/uploadFiles/文件夹下

   3、使用绝对路径,文件上传到d://uploadFiles/文件夹下

/**
 * 单文件上传
 * @param req
 * @param multiReq
 */
@RequestMapping(value = "/testUploadFile", method = RequestMethod.POST)
public void testUploadFile(HttpServletRequest req,
                           MultipartHttpServletRequest multiReq) {
    // 获取上传文件的路径,uploadFilePath 其实是文件带后缀的名字
    String uploadFilePath = multiReq.getFile("file1").getOriginalFilename();
    System.out.println("uploadFlePath:" + uploadFilePath);
    // 截取上传文件的文件名
    String uploadFileName = uploadFilePath.substring(
            uploadFilePath.lastIndexOf(‘\\‘) + 1, uploadFilePath.indexOf(‘.‘));
    System.out.println("multiReq.getFile()" + uploadFileName);
    // 截取上传文件的后缀
    String uploadFileSuffix = uploadFilePath.substring(
            uploadFilePath.indexOf(‘.‘) + 1, uploadFilePath.length());
    System.out.println("uploadFileSuffix:" + uploadFileSuffix);
    FileOutputStream fos = null;
    FileInputStream fis = null;
    try {
        //1、使用相对路径
        //获取跟目录,文件保存路径为D:/fxglxt/target/classes/static/web/uploadFiles
       //  File path = new File(ResourceUtils.getURL("classpath:").getPath());
       //  if(!path.exists()) path = new File("");
       //  System.out.println("path:"+path.getAbsolutePath());
       //
       // //如果上传目录为/static/images/upload/,则可以如下获取:
       //  File upload = new File(path.getAbsolutePath(),"static/web/uploadFiles/");
       //  if(!upload.exists()) upload.mkdirs();
       //  System.out.println("upload url:"+upload.getAbsolutePath());
       //
       //
       //  fis = (FileInputStream) multiReq.getFile("file1").getInputStream();
       //  fos = new FileOutputStream(new File(path.getAbsolutePath(),"static/web/uploadFiles/"+ uploadFileName
       //          + ".")
       //          + uploadFileSuffix);
        
        //2、使用绝对路径,文件上传到D:/fxglxt/src/main/resources/static/web/uploadFiles/文件夹下
        String c=System.getProperty("user.dir");
        System.out.println("c"+c);

        fis = (FileInputStream) multiReq.getFile("file1").getInputStream();
        fos = new FileOutputStream(new File(c+"/src/main/resources/static/web/uploadFiles/" + uploadFileName
                + ".")
                + uploadFileSuffix);

        //3、使用绝对路径,文件上传到d://uploadFiles/文件夹下
        // fis = (FileInputStream) multiReq.getFile("file1").getInputStream();
        // fos = new FileOutputStream(new File(c+"d://uploadFiles/" + uploadFileName
        //         + ".")
        //         + uploadFileSuffix);


        System.out.println("fos"+fos);
        byte[] temp = new byte[1024];
        int i = fis.read(temp);
        while (i != -1){
            fos.write(temp,0,temp.length);
            fos.flush();
            i = fis.read(temp);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

  

  

springboot文件上传--单文件上传

标签:ansi   type   byte   ast   文件上传   相对路径   pos   family   files   

原文地址:https://www.cnblogs.com/xuemeng11/p/13143170.html


评论


亲,登录后才可以留言!