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的方式解决。(有时间再补充)

  • 相关阅读:
    【iOS】打印方法名
    【iOS】设备系统版本
    【iOS】receiver type *** for instance message is a forward declaration
    【iOS】获取应用程序本地路径
    hash算法
    redis文档
    Couchbase
    nodejs多核处理
    基于nodejs的消息中心
    nodejs两个例子
  • 原文地址:https://www.cnblogs.com/jirglt/p/3480810.html
Copyright © 2011-2022 走看看