abi*_*964 5 java servlets file-upload
我正在上传一个文件,我想提供一个相对路径,因为该程序应该在linux和windows环境下工作.
这就是我用来上传的内容
realPath = getServletContext().getRealPath(/files);
destinationDir = new File(realPath);
if(!item.isFormField())
{
File file = new File(destinationDir,item.getName());
item.write(file);
}
Run Code Online (Sandbox Code Playgroud)
在这里直接提供相对路径的任何其他方法
File file = new File(destinationDir,item.getName());
Run Code Online (Sandbox Code Playgroud)
我想提供一个相对路径
永远不要在webapp中这样做.工作目录不能从代码内部控制.
因为该程序应该在linux和windows环境下工作.
只是用"/path/to/uploaded/files".它在两种环境中都同样有效.在Windows中,它只与服务器运行的磁盘相同.
File file = new File("/path/to/uploaded/files", filename);
// ...
Run Code Online (Sandbox Code Playgroud)
这就是我用来上传的内容
Run Code Online (Sandbox Code Playgroud)realPath = getServletContext().getRealPath(/files); destinationDir = new File(realPath);
您不应将文件存储在webcontent中.当WAR未展开时,这将失败,即使是这样,每当您重新部署WAR时,所有文件都将丢失.将它们存放在WAR外的绝对位置,例如/path/to/uploaded/files之前建议的那样.
| 归档时间: |
|
| 查看次数: |
19943 次 |
| 最近记录: |