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

  • 相关阅读:
    python2和python3的区别
    开发常用命令
    类中的内置方法
    while 循环
    sql执行效率,explain 查询执行效率
    jmeter 中的 HTTP URL Re-writing Modifier
    jmeter beanshell内容
    jmeter 和 ajax
    jdbc 与 each controller 对多条查询结果的处理
    jdbc与 Beanshell PostProcessor 对多条结果的处理
  • 原文地址:https://www.cnblogs.com/jirglt/p/3480810.html
Copyright © 2011-2022 走看看