2016-08-10 64 views
0

我正在研究Java應用程序,我想要做的是向用戶提供上傳圖像並在其個人資料中查看的功能。我知道類似的問題已經回答了很多次,但這是我第一次這樣做,而且我真的很努力使它工作。Jsp從webapps加載圖像ROOT文件夾

所以這是我的測試代碼:

upload.jsp

... 
<body> 
    <form method="post" action="FileUploader" encType="multipart/form-data"> 
     <input type="file" name="file" value="select images..."/> 
     <input type="submit" value="start upload"/> 
    </form> 
</body> 
... 

FileUploader.java

正如你所看到的,我在我的所有圖像保存在Tomcat的webapps/ROOT/files夾。

@WebServlet("/FileUploader") 
public class FileUploader extends HttpServlet { 

    private static final long serialVersionUID = 1L; 

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     PrintWriter out = response.getWriter(); 

     if(!ServletFileUpload.isMultipartContent(request)){ 
      out.println("Nothing to upload"); 
      return; 
     } 
     FileItemFactory itemfactory = new DiskFileItemFactory(); 
     ServletFileUpload upload = new ServletFileUpload(itemfactory); 
     try{ 
      List<FileItem> items = upload.parseRequest(new ServletRequestContext(request)); 
      for(FileItem item:items){ 

       String contentType = item.getContentType(); 
       if(!contentType.equals("image/png")){ 
        out.println("only png format image files supported"); 
        continue; 
       } 
       File uploadDir = new File("/home/agg/apache-tomcat/webapps/ROOT/files"); 
       File file = File.createTempFile("img",".png",uploadDir); 
       item.write(file); 

       out.println("Filename: " + file.getName()); 
       out.println("File Saved Successfully"); 

       response.sendRedirect("message.jsp"); 
      } 
     } 
     catch(FileUploadException e){ 
      out.println("upload fail"); 
     } 
     catch(Exception ex){ 
      out.println("can't save"); 
     } 
    } 
} 

message.jsp

在這裏,我試圖加載通過另一個servlet保存的圖像之一。

... 
<body> 
    <img src="file/img1.png"> 
</body> 

FileServlet.java

的Servlet檢索圖像。

@WebServlet("/file/*") 
public class FileServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB. 
    private String filePath; 

    public void init() throws ServletException { 
     this.filePath = "/files"; 
    } 

    protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     System.out.println("================In do get=================="); 

     // Get requested file by path info. 
     String requestedFile = request.getPathInfo(); 
     System.out.println("Requested File: " + requestedFile); 

     // Check if file is actually supplied to the request URI. 
     if (requestedFile == null) { 
      response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404. 
      return; 
     } 

     // Decode the file name (might contain spaces and on) and prepare file object. 
     File file = new File(filePath, URLDecoder.decode(requestedFile, "UTF-8")); 

     System.out.println("Filename: " + file.getName()); 
     System.out.println(file.getAbsolutePath()); 

     // Check if file actually exists in filesystem. 
     if (!file.exists()) { 
      System.out.println("DOES NOT EXIST"); 
      response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404. 
      return; 
     } 

    // more code here but it does not matter 
    } 
} 

問題是圖像無法加載。在控制檯上打印的路徑似乎是正確的。我在這裏錯過了什麼?

回答

0

對我來說,你的問題是在這裏

<img src="file/img1.png"> 

它似乎並沒有成爲下一個理由的正確路徑:

  1. 根目錄/.../webapps/ROOT/files所以它應該與files開始不file
  2. 該文件的名稱應該是img +隨機長+ .png這裏您將隨機的長爲1,這似乎不正確
  3. 因爲它是在ROOT web應用程序,你應該情願把絕對路徑而不是相對路徑,換句話說路徑應該以斜槓開始