2016-05-24 70 views
0

我存储由用户上传的文件内容如下:用的Apache Tomcat在哪里存储上传的静态文件

InputStream inputStream = null; 
OutputStream outputStream = null; 

MultipartFile file = uploadedFile.getFile(); 

String fileName = file.getOriginalFilename(); 
String path ="C:/apache-tomcat-8.0.18/uploads/images/"; 

try { 
    inputStream = file.getInputStream(); 
    File newFile = new File(path +fileName); 
    if (!newFile.exists()) { 
    System.out.println(newFile.getAbsolutePath()); 
    newFile.createNewFile(); 
    } 
    outputStream = new FileOutputStream(newFile); 
    int read = 0; 
    byte[] bytes = new byte[1024]; 

    while ((read = inputStream.read(bytes)) != -1) { 
    outputStream.write(bytes, 0, read); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
finally { 
    outputStream.close(); 
} 

什么必须在上传的文件应当保存的路径?我想存储图像并将它们显示在JSP页面中。

我最初将文件存储在我的Maven项目的webapp/images文件夹中。但是,当应用程序重新部署时,上传的文件也将被删除。我怎么解决这个问题?

当我上传C:/apache-tomcat-8.0.18/uploads/images/我无法获取图像显示在我的JSP页面。获取错误不允许加载本地资源

+0

当你的JSP在浏览器中获取呈现,什么是对图像的HTML代码(''​​)是什么样子? – manish

回答

0

将文件存储在磁盘上的最佳方式是使用Content Repository API for Java(JCR)。我建议Jackrabbit。

看看这个答案Recommended way to save uploaded files in a servlet application

+0

我看了一下链接 有'File uploads = new File(“/ path/to/uploads”);' 但是我不会忘记我必须写什么路径?它不在我的webapp中吗? –

相关问题