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

 

 

posted on 2013-12-18 17:57  专注2018  阅读(307)  评论(0编辑  收藏  举报