为什么里面的eclipse浏览器能够上传和读取图像文件,而像Chrome这样的浏览器能够上传却无法读取图像文件?

时间:2017-06-10 11:22:21

标签: java html jsp servlets

我有三个代码文件,其中两个是jsp文件,另一个是servlet文件,如下所示:

servlet文件UploadServlet.java

package net.codejava.servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet("/UploadServlet")
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
                 maxFileSize=1024*1024*10,      // 10MB
                 maxRequestSize=1024*1024*50)   // 50MB
public class UploadServlet extends HttpServlet {

    /**
     * Name of the directory where uploaded files will be saved, relative to
     * the web application directory.
     */
    private static final String SAVE_DIR = "uploadFiles";

    /**
     * handles file upload
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // gets absolute path of the web application
        String appPath = request.getServletContext().getRealPath("");
        // constructs path of the directory to save uploaded file
        String savePath = appPath + File.separator + SAVE_DIR+ File.separator +"kala";

        // creates the save directory if it does not exists
        File fileSaveDir = new File(savePath);
        if (!fileSaveDir.exists()) {
            fileSaveDir.mkdir();
        }
        System.out.println(savePath);
        //savePath="C:\\Users\\SANJAY GUPTA\\Downloads\\eclipse";
        for (Part part : request.getParts()) {
            String fileName = extractFileName(part);
            // refines the fileName in case it is an absolute path
            fileName = new File(fileName).getName();
            part.write(savePath + File.separator + fileName);
        }

        request.setAttribute("message", "Upload has been done successfully!");
        getServletContext().getRequestDispatcher("/message.jsp").forward(
                request, response);
    }

    /**
     * Extracts file name from HTTP header content-disposition
     */
    private String extractFileName(Part part) {
        String contentDisp = part.getHeader("content-disposition");
        String[] items = contentDisp.split(";");
        for (String s : items) {
            if (s.trim().startsWith("filename")) {
                return s.substring(s.indexOf("=") + 2, s.length()-1);
            }
        }
        return "";
    }
}

第一个jsp文件index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Upload</title>
</head>
<body>
<center>
    <h1>File Upload</h1>
    <form method="post" action="UploadServlet"
        enctype="multipart/form-data">
        Select file to upload: <input type="file" name="file" size="60" /><br />
        <br /> <input type="submit" value="Upload" />
    </form>
</center>
</body>
</html>

第二个jsp文件message.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload</title>
</head>
<body>
    <h2>${requestScope.message}</h2>
    <%@ page import="java.io.File"%>
    <%
     String SAVE_DIR = "uploadFiles";String appPath = request.getServletContext().getRealPath("");
     String savePath = appPath + File.separator + SAVE_DIR+ File.separator +"kala"+ File.separator;

     out.write("<h3>This is the Profile Picture</h3><br><br><button type=\"submit\" name=\"img\">"+
                "<img src=\""+savePath+"333.jpg\" style=\"width:304px;height:228px;\">"+
                "</button>"); 

    %>
</body>
</html>

运行这些文件代码我正在使用eclipse IDE,在eclipse IDE中我能够查看333.jpg图像并能够上传其他文件,但是当我从eclipse复制本地主机URL并运行程序时chrome浏览器我能够上传文件,但无法查看已上传到指定路径的333.jpg图像文件。

我想知道为什么会这样,它的解决方案是什么?

1 个答案:

答案 0 :(得分:0)

只需更改message.jsp文件

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>Upload</title>
 </head>
 <body>
<h2>${requestScope.message}</h2>
<%@ page import="java.io.File"%>
<%
 String SAVE_DIR = "uploadFiles";
 String appPath =  request.getContextPath(); 
 String savePath = appPath + File.separator + SAVE_DIR+ File.separator +"kala"+ File.separator;

 out.write("<h3>This is the Profile Picture</h3><br><br><button type=\"submit\" name=\"img\">"+
            "<img src=\""+savePath+"333.jpg\" style=\"width:304px;height:228px;\">"+
            "</button>"); 

%>