- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我读了here那个人无论如何都不应该将文件保存在服务器中,因为它不可移植、事务性强并且需要外部参数。但是,考虑到我需要一个用于 tomcat (7) 的 tmp 解决方案,并且我对我想知道的服务器机器具有(相对)控制权:
保存文件的最佳位置是什么?我应该将它保存在 /WEB-INF/uploads
(建议反对 here )或 $CATALINA_BASE
下的某个地方(参见 here )或...? JavaEE 6 教程 gets the path from the user (:wtf:)。注意:该文件不应以任何方式下载。
我是否应该按照详细说明设置配置参数 here ?我会欣赏一些代码(我宁愿给它一个相对路径 - 所以它至少是 Tomcat 可移植的)- Part.write()
看起来很有希望——但显然需要一个绝对路径
我对这种方法相对于数据库/JCR 存储库的缺点的阐述很感兴趣
不幸的是FileServlet @BalusC 专注于下载文件,而他的 answer关于上传文件的部分跳过了关于文件保存位置的部分。
最好是可以轻松转换为使用 DB 或 JCR 实现(如 jackrabbit)的解决方案。
最佳答案
将其存储在 IDE 项目文件夹(即服务器的部署文件夹)的可访问位置除外,原因在 Uploaded image only available after refreshing the page 的回答中提到。 :
Changes in the IDE's project folder does not immediately get reflected in the server's work folder. There's kind of a background job in the IDE which takes care that the server's work folder get synced with last updates (this is in IDE terms called "publishing"). This is the main cause of the problem you're seeing.
In real world code there are circumstances where storing uploaded files in the webapp's deploy folder will not work at all. Some servers do (either by default or by configuration) not expand the deployed WAR file into the local disk file system, but instead fully in the memory. You can't create new files in the memory without basically editing the deployed WAR file and redeploying it.
Even when the server expands the deployed WAR file into the local disk file system, all newly created files will get lost on a redeploy or even a simple restart, simply because those new files are not part of the original WAR file.
对我或其他任何人来说,将其保存在本地磁盘文件系统的确切位置并不重要,只要您 do not ever use getRealPath()
method .使用该方法在任何情况下都令人担忧。
存储位置的路径又可以用多种方式定义。您必须自己全部完成。也许这就是您的困惑所在,因为您以某种方式期望服务器自动完成所有这些工作。请注意 @MultipartConfig(location)
未指定最终上传目的地,但案例文件大小的临时存储位置超过内存存储阈值。
因此,可以通过以下任一方式定义最终存储位置的路径:
硬编码:
File uploads = new File("/path/to/uploads");
环境变量来自 SET UPLOAD_LOCATION=/path/to/uploads
:
File uploads = new File(System.getenv("UPLOAD_LOCATION"));
服务器启动期间的虚拟机参数 -Dupload.location="/path/to/uploads"
:
File uploads = new File(System.getProperty("upload.location"));
*.properties
文件条目为 upload.location=/path/to/uploads
:
File uploads = new File(properties.getProperty("upload.location"));
web.xml
<context-param>
名字upload.location
和值(value)/path/to/uploads
:
File uploads = new File(getServletContext().getInitParameter("upload.location"));
如果有的话,使用服务器提供的位置,例如在 JBoss AS/WildFly :
File uploads = new File(System.getProperty("jboss.server.data.dir"), "uploads");
无论哪种方式,您都可以按如下方式轻松引用和保存文件:
File file = new File(uploads, "somefilename.ext");
try (InputStream input = part.getInputStream()) {
Files.copy(input, file.toPath());
}
或者,当您想要自动生成一个唯一的文件名以防止用户覆盖巧合名称相同的现有文件时:
File file = File.createTempFile("somefilename-", ".ext", uploads);
try (InputStream input = part.getInputStream()) {
Files.copy(input, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
如何获取part
JSP/Servlet 中的答案在 How to upload files to server using JSP/Servlet? 中以及如何获得part
在 JSF 中得到回答 How to upload file using JSF 2.2 <h:inputFile>? Where is the saved File?
注意:不要使用 Part#write()
因为它解释相对于 @MultipartConfig(location)
中定义的临时存储位置的路径.还要绝对确保您不会通过错误地使用 Reader
在读/写期间将字节转换为字符来破坏二进制文件,例如 PDF 文件或图像文件。/Writer
而不是 InputStream
/OutputStream
.
关于tomcat - 在 servlet 应用程序中保存上传文件的推荐方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35190093/
我正在通过 labrepl 工作,我看到了一些遵循此模式的代码: ;; Pattern (apply #(apply f %&) coll) ;; Concrete example user=> (a
我从未向应用商店提交过应用,但我会在不久的将来提交。 到目前为止,我对为 iPhone 而非 iPad 进行设计感到很自在。 我了解,通过将通用PAID 应用放到应用商店,客户只需支付一次就可以同时使
我有一个应用程序,它使用不同的 Facebook 应用程序(2 个不同的 AppID)在 Facebook 上发布并显示它是“通过 iPhone”/“通过 iPad”。 当 Facebook 应用程序
我有一个要求,我们必须通过将网站源文件保存在本地 iOS 应用程序中来在 iOS 应用程序 Webview 中运行网站。 Angular 需要服务器来运行应用程序,但由于我们将文件保存在本地,我们无法
所以我有一个单页客户端应用程序。 正常流程: 应用程序 -> OAuth2 服务器 -> 应用程序 我们有自己的 OAuth2 服务器,因此人们可以登录应用程序并获取与用户实体关联的 access_t
假设我有一个安装在用户设备上的 Android 应用程序 A,我的应用程序有一个 AppWidget,我们可以让其他 Android 开发人员在其中以每次安装成本为基础发布他们的应用程序推广广告。因此
Secrets of the JavaScript Ninja中有一个例子它提供了以下代码来绕过 JavaScript 的 Math.min() 函数,该函数需要一个可变长度列表。 Example:
当我分别将数组和对象传递给 function.apply() 时,我得到 NaN 的 o/p,但是当我传递对象和数组时,我得到一个数字。为什么会发生这种情况? 由于数组也被视为对象,为什么我无法使用它
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章ASP转换格林威治时间函数DateDiff()应用由作者收集整理,如果你
我正在将列表传递给 map并且想要返回一个带有合并名称的 data.frame 对象。 例如: library(tidyverse) library(broom) mtcars %>% spl
我有一个非常基本的问题,但我不知道如何实现它:我有一个返回数据框,其中每个工具的返回值是按行排列的: tmp<-as.data.frame(t(data.frame(a=rnorm(250,0,1)
我正在使用我的 FB 应用创建群组并邀请用户加入我的应用群组,第一次一切正常。当我尝试创建另一个组时,出现以下错误: {"(OAuthException - #4009) (#4009) 在有更多用户
我们正在开发一款类似于“会说话的本”应用程序的 child 应用程序。它包含大量用于交互式动画的 JPEG 图像序列。 问题是动画在 iPad Air 上播放正常,但在 iPad 2 上播放缓慢或滞后
我关注 clojure 一段时间了,它的一些功能非常令人兴奋(持久数据结构、函数式方法、不可变状态)。然而,由于我仍在学习,我想了解如何在实际场景中应用,证明其好处,然后演化并应用于更复杂的问题。即,
我开发了一个仅使用挪威语的应用程序。该应用程序不使用本地化,因为它应该仅以一种语言(挪威语)显示。但是,我已在 Info.plist 文件中将“本地化 native 开发区域”设置为“no”。我还使用
读完 Anthony's response 后上a style-related parser question ,我试图说服自己编写单体解析器仍然可以相当紧凑。 所以而不是 reference ::
multicore 库中是否有类似 sapply 的东西?还是我必须 unlist(mclapply(..)) 才能实现这一点? 如果它不存在:推理是什么? 提前致谢,如果这是一个愚蠢的问题,我们深表
我喜欢在窗口中弹出结果,以便更容易查看和查找(例如,它们不会随着控制台继续滚动而丢失)。一种方法是使用 sink() 和 file.show()。例如: y <- rnorm(100); x <- r
我有一个如下所示的 spring mvc Controller @RequestMapping(value="/new", method=RequestMethod.POST) public Stri
我正在阅读 StructureMap关于依赖注入(inject),首先有两部分初始化映射,具体类类型的接口(interface),另一部分只是实例化(请求实例)。 第一部分需要配置和设置,这是在 Bo
我是一名优秀的程序员,十分优秀!