zoukankan      html  css  js  c++  java
  • java 上传附件的一点积累

    1、第一种:

    File inFile = new File(downfileA);//downfileA是前台传过来的,文件路径
    String fileName = inFile.getName();
    String path = request.getSession().getServletContext().getRealPath("/");
    File outFile = new File(path);
    FileUtils.copyFile(new File(fileName), outFile);

    2、第二种:

    InputStream is = null;
    OutputStream os = null;
    byte[] number = new byte[1024];
    try
    {
              File inFile = new File(downfileA);//downfileA是前台传过来的,文件路径
              String fileName = inFile.getName();
              String path = request.getSession().getServletContext().getRealPath("/")+fileName;
              is = new BufferedInputStream(new FileInputStream(downfileA), 1024);
              os = new BufferedOutputStream(new FileOutputStream(path),1024);
              while(is.read(number) > 0)
              {
                       os.write(number);
              }
              os.close();
              is.close();
    }
    catch (Exception ex)
    {
              ex.printStackTrace();
    }

    注:上述两种方法在linux下不好用。会提示找不到路径。

    3、在linux下通过servlet的方式解决。(有时间再补充)

  • 相关阅读:
    57. Insert Interval
    287. Find the Duplicate Number
    52. N-Queens II
    51. N-Queens
    151. Reverse Words in a String
    29. Divide Two Integers
    [POJ2104]K-th Number
    [JSOI2008]最大数
    [BZOJ3673&3674]可持久化并查集&加强版
    C++ STL rope介绍----可持久化平衡树
  • 原文地址:https://www.cnblogs.com/jirglt/p/3480810.html
Copyright © 2011-2022 走看看