为什么图像显示在服务器上而不是本地主机上?
Why are images shown on server but not on localhost?
图像通过以下 HTML.
上传到部署服务器时正确显示
<img width="516" height="730" class="theCanvas img-fluid" style="transform-origin: 50% 50%;
transition:transform 200ms ease-in-out; cursor: move; transform: matrix(1, 0, 0, 1, 0, 0);"
draggable="false" src="../uploads/Scans/uploadedImage.jpg">
但是当 运行 来自本地主机(Eclipse,Tomcat)的应用程序并从那里上传图像时,图像不显示。
编辑源码也不显示(通过F12开发工具编辑src标签)
http://localhost:8080/contextRoot/uploads/Scans/uploadedImage.jpg
即使我尝试将完整的源代码提供给文件在 Eclipse 中的位置,它也不会显示
C:\Users\myUser\eclipse-workspace\contextRoot\uploads\Scans\uploadedImage.jpg
(虽然我认为这最后一个问题是由于跨域问题,就好像我尝试使用 jquery 更改 src 属性然后我得到 'Access Denied')
有什么想法吗?
以下内容没有回答为什么本地环境和服务器应该以不同方式工作的问题,但它确实提供了一个可行的解决方案。 (代码基于 Ali Irawan's solution )
写一个servlet来获取图片,然后用
这样的代码显示图片
$('#image').attr("src","display?id="+result);
servlet 将在 web.xml
中定义
<servlet-name>DisplayServlet</servlet-name>
<servlet-class>com.web.DisplayServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayServlet</servlet-name>
<url-pattern>/display</url-pattern>
</servlet-mapping>
<servlet>
servlet 看起来像
public class DisplayServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -7598483378197199569L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = SecurityIssues.paramEncode(request.getParameter("id"));
response.setContentType("image/jpeg");
String fileFullPath = "C:\exampleDir\" + id + ".jpg";
File my_file = new File(fileFullPath);
// This should send the file to browser
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(my_file);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
}
图像通过以下 HTML.
上传到部署服务器时正确显示<img width="516" height="730" class="theCanvas img-fluid" style="transform-origin: 50% 50%;
transition:transform 200ms ease-in-out; cursor: move; transform: matrix(1, 0, 0, 1, 0, 0);"
draggable="false" src="../uploads/Scans/uploadedImage.jpg">
但是当 运行 来自本地主机(Eclipse,Tomcat)的应用程序并从那里上传图像时,图像不显示。
编辑源码也不显示(通过F12开发工具编辑src标签)
http://localhost:8080/contextRoot/uploads/Scans/uploadedImage.jpg
即使我尝试将完整的源代码提供给文件在 Eclipse 中的位置,它也不会显示
C:\Users\myUser\eclipse-workspace\contextRoot\uploads\Scans\uploadedImage.jpg
(虽然我认为这最后一个问题是由于跨域问题,就好像我尝试使用 jquery 更改 src 属性然后我得到 'Access Denied')
有什么想法吗?
以下内容没有回答为什么本地环境和服务器应该以不同方式工作的问题,但它确实提供了一个可行的解决方案。 (代码基于 Ali Irawan's solution )
写一个servlet来获取图片,然后用
这样的代码显示图片$('#image').attr("src","display?id="+result);
servlet 将在 web.xml
中定义 <servlet-name>DisplayServlet</servlet-name>
<servlet-class>com.web.DisplayServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayServlet</servlet-name>
<url-pattern>/display</url-pattern>
</servlet-mapping>
<servlet>
servlet 看起来像
public class DisplayServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -7598483378197199569L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = SecurityIssues.paramEncode(request.getParameter("id"));
response.setContentType("image/jpeg");
String fileFullPath = "C:\exampleDir\" + id + ".jpg";
File my_file = new File(fileFullPath);
// This should send the file to browser
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(my_file);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
}