fileFileName = new Date().getTime() + fileFileName; ServletContext servletContext = ServletActionContext.getServletContext(); String path = servletContext.getRealPath("/");// 文件最终要上传到的路径 System.out.println("絕對路徑"+path+"headphoto\"+fileFileName); FileOutputStream out = new FileOutputStream(path+"headphoto\"+fileFileName); FileInputStream in = new FileInputStream(file); byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } out.close();
in.clos();
1.路径:servletContext.getRealPath("/")+文件夹名+文件名 newy一个fileoutputstream;
2.写入文件:是从struts控件里面获取的文件new一个fileinputstream;
3.标准写法:
byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); }