在 Tomcat 中时,GWT 无法从文件系统加载图形
我使用以下 Servlet 来映射文件系统文件夹中的图像。
public class ImageServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
// Get the base path for graphics root folder
String basePath = System.getenv(Constants.ENV_VAR);
String specifier = req.getParameter(Constants.PARAMETER);
resp.setContentType(Constants.CONTENT_TYPE);
File file = new File(basePath+specifier+"."+Constants.IMG_TYPE);
resp.setContentLength((int)file.length());
FileInputStream in = new FileInputStream(file);
OutputStream out = resp.getOutputStream();
// Copy the contents of the file to the output stream
byte[] buf = new byte[Constants.INPUT_BUFFER_SIZE];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
}
}
然后,我使用 GWT-RPC 来获取图像,方法是使用以下 AppServiceImpl 类:
public class AppServiceImpl extends RemoteServiceServlet implements AppService {
//Get the base path for graphics root folder
String basePath = System.getenv(Constants.ENV_VAR);
@Override
public ArrayList<String> getImageFiles(String folder) {
ArrayList<String> list = new ArrayList<String>();
File parent = new File(**basePath+folder**);
if (!parent.isDirectory()) {
return list;
}
File[] files = parent.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile() && !files[i].isHidden()) {
// save image title
list.add(files[i].getName().substring(0, files[i].getName().indexOf(".")));
}
}
return list;
}
...
为了在 GWT 代码中获取图像,我使用: final Image lgImg = new Image(Constants.URL_PREFIX + imgPath);
:
URL_PREFIX = "http://localhost:8888/images?" + 参数 + "="
ENV_VAR = "APP_GFX_HOME"
PARAMETER = " path"
在开发模式下,一切工作正常,但是当我部署到 tomcat 服务器时,图像不会出现,我需要一些帮助找出问题。
ps:这是web.xml文件的一部分
<!-- Services -->
<servlet>
<servlet-name>theImageServlet</servlet-name>
<servlet-class>com.app.server.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>theImageServlet</servlet-name>
<url-pattern>/images</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>appService</servlet-name>
<servlet-class>com.app.server.AppServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>appService</servlet-name>
<url-pattern>/app/services</url-pattern>
</servlet-mapping>
I use the following Servlet to map images from a folder of the file-system.
public class ImageServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
// Get the base path for graphics root folder
String basePath = System.getenv(Constants.ENV_VAR);
String specifier = req.getParameter(Constants.PARAMETER);
resp.setContentType(Constants.CONTENT_TYPE);
File file = new File(basePath+specifier+"."+Constants.IMG_TYPE);
resp.setContentLength((int)file.length());
FileInputStream in = new FileInputStream(file);
OutputStream out = resp.getOutputStream();
// Copy the contents of the file to the output stream
byte[] buf = new byte[Constants.INPUT_BUFFER_SIZE];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
}
}
Then, I use GWT-RPC to get images, by using the following AppServiceImpl class:
public class AppServiceImpl extends RemoteServiceServlet implements AppService {
//Get the base path for graphics root folder
String basePath = System.getenv(Constants.ENV_VAR);
@Override
public ArrayList<String> getImageFiles(String folder) {
ArrayList<String> list = new ArrayList<String>();
File parent = new File(**basePath+folder**);
if (!parent.isDirectory()) {
return list;
}
File[] files = parent.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile() && !files[i].isHidden()) {
// save image title
list.add(files[i].getName().substring(0, files[i].getName().indexOf(".")));
}
}
return list;
}
...
In order to get an image in GWT code, i use: final Image lgImg = new Image(Constants.URL_PREFIX + imgPath);
where:
URL_PREFIX = "http://localhost:8888/images?" + PARAMETER + "="
ENV_VAR = "APP_GFX_HOME"
PARAMETER = "path"
In development mode, all work good, but when i deploy to a tomcat server, images do not appear, and I need some help to figure out the problem.
p.s: Here is part of web.xml file
<!-- Services -->
<servlet>
<servlet-name>theImageServlet</servlet-name>
<servlet-class>com.app.server.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>theImageServlet</servlet-name>
<url-pattern>/images</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>appService</servlet-name>
<servlet-class>com.app.server.AppServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>appService</servlet-name>
<url-pattern>/app/services</url-pattern>
</servlet-mapping>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会对 URL 进行硬编码,因为上下文部分在开发模式下不存在。
只需使用:
我在类似的情况下使用它从报告 servlet 下载 Excel 文件,并且它在所有情况下都能正常工作。
I wouldn't hard-code the URL, because the context portion is not present in dev mode.
Just use:
I use this in a similar situation to download Excel files from a reporting servlet and it works fine in all situations.