如何将图片保存到NetBeans企业应用的项目固定目录?
问题描述
我正在NetBeans中开发企业应用,项目目录结构如下:
-Web Pages -resources -images -products (我希望保存文件的位置) -Source Package -com.mb -productsMB.java (我的函数所在类)
现有代码可正常运行,但目前是将图片保存到指定磁盘路径:
public String addProducts() { try { filename = file.getSubmittedFileName(); InputStream input; input = file.getInputStream(); FileOutputStream output = new FileOutputStream("D:\\Driver\\"+filename); byte[] buf = new byte[1024]; int len; while ((len = input.read(buf)) > 0) { output.write(buf, 0, len); } product.setImage(filename); productsFacade.create(product); } catch (IOException ex) { Logger.getLogger(ProductsMB.class.getName()).log(Level.SEVERE, null, ex); } return "products"; }
我希望修改FileOutputStream的路径,将文件保存到项目内的Web Pages/resources/images/products目录,且无需指定项目所在磁盘。
我尝试了以下方法,可运行但文件仅保存到临时目录,停止运行项目后图片即丢失:
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); ServletContext servletContext = (ServletContext) externalContext.getContext(); String uploadDirectory = servletContext.getRealPath("/resources/images/products"); String filePath = uploadDirectory + "/" + filename; FileOutputStream output = new FileOutputStream(filePath);
解决方案
临时目录文件丢失的原因
servletContext.getRealPath()获取的是项目运行时的部署目录,这个目录是服务器(如Tomcat)启动时解压项目包生成的临时目录,服务器停止或重新部署时会被清空/覆盖,因此文件无法持久保存。
方案1:保存到项目源码目录(适合开发阶段)
NetBeans运行项目时,user.dir系统属性会指向项目根目录,利用这个属性拼接目标源码路径即可:
public String addProducts() { try { filename = file.getSubmittedFileName(); // 获取项目根目录 String projectRoot = System.getProperty("user.dir"); // 拼接目标源码目录路径 String targetDir = projectRoot + "/Web Pages/resources/images/products"; // 自动创建不存在的目录 File dir = new File(targetDir); if (!dir.exists()) { dir.mkdirs(); } String filePath = targetDir + "/" + filename; // 使用try-with-resources自动关闭流,避免资源泄漏 try (InputStream input = file.getInputStream(); FileOutputStream output = new FileOutputStream(filePath)) { byte[] buf = new byte[1024]; int len; while ((len = input.read(buf)) > 0) { output.write(buf, 0, len); } } product.setImage(filename); productsFacade.create(product); } catch (IOException ex) { Logger.getLogger(ProductsMB.class.getName()).log(Level.SEVERE, null, ex); } return "products"; }
方案2:使用外部独立存储目录(生产环境推荐)
直接保存到源码目录在团队协作或生产环境中并不合理,更规范的做法是使用独立的外部存储目录:
- 在项目外创建专用存储目录(如
D:/app_uploads/products/) - 在
web.xml中配置该路径:
<context-param> <param-name>productImageDir</param-name> <param-value>D:/app_uploads/products/</param-value> </context-param>
- 代码中读取配置并保存文件:
public String addProducts() { try { filename = file.getSubmittedFileName(); ServletContext servletContext = FacesContext.getCurrentInstance().getExternalContext().getContext(); // 读取配置的存储目录 String targetDir = servletContext.getInitParameter("productImageDir"); File dir = new File(targetDir); if (!dir.exists()) { dir.mkdirs(); } String filePath = targetDir + "/" + filename; try (InputStream input = file.getInputStream(); FileOutputStream output = new FileOutputStream(filePath)) { byte[] buf = new byte[1024]; int len; while ((len = input.read(buf)) > 0) { output.write(buf, 0, len); } } product.setImage(filename); productsFacade.create(product); } catch (IOException ex) { Logger.getLogger(ProductsMB.class.getName()).log(Level.SEVERE, null, ex); } return "products"; }
额外注意事项
- 务必确保存储目录有足够的读写权限,避免权限不足导致文件保存失败
- 优先使用
try-with-resources语法管理流资源,避免内存泄漏
内容的提问来源于stack exchange,提问作者Toma im
相关产品推荐
相关产品推荐

