Java 本地路径的OSGi httpService.registerResources()
我正在尝试注册一个本地路径资源(在OSGi包之外),但它就是不起作用。 这很奇怪,因为除此之外,如何使用jar中尚未存在的资源 这是我的密码:Java 本地路径的OSGi httpService.registerResources(),java,http,server,osgi,httpservice,Java,Http,Server,Osgi,Httpservice,我正在尝试注册一个本地路径资源(在OSGi包之外),但它就是不起作用。 这很奇怪,因为除此之外,如何使用jar中尚未存在的资源 这是我的密码: protected void bindHttpService(HttpService httpService) { httpService.registerResources("/testServer/resources", "/resources", null); httpService.registerResourc
protected void bindHttpService(HttpService httpService)
{
httpService.registerResources("/testServer/resources", "/resources", null);
httpService.registerResources("/testServer/pictures", "C:\\mypath\\config\\", null);
第一个很好地工作(但由于资源在jar中,所以无法更改),但是如果我放置一个本地路径,它就会忽略它并返回“not found”。我使用了一个单独的servlet,我在httpService中注册了该servlet:
httpService.registerServlet("/testServer/pictures", new ImageServlet("C:\myPics\pics"), null, null);
servlet是我很快找到的一些代码,因此它可能不是最好的实现:
public class ImageServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
private String imgPath;
public ImageServlet(String picPath){
super();
imgPath = picPath;
}
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
ServletContext cntx= req.getServletContext();
// Get the absolute path of the image
// retrieve mimeType dynamically
String filename = imgPath+ "/testpicture2.jpg";
String mime = cntx.getMimeType(imgPath+ "/testpicture2.jpg");
if (mime == null) {
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
resp.setContentType(mime);
File file = new File(filename);
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[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
out.close();
in.close();
}
我仍然在想,如果不使用这个变通方法(这是在另一个答案中建议的),我如何能够使用绝对路径获取本地资源,因此如果您仍然能够帮助我,那就太好了 我使用了一个在httpService中注册的单独servlet:
httpService.registerServlet("/testServer/pictures", new ImageServlet("C:\myPics\pics"), null, null);
servlet是我很快找到的一些代码,因此它可能不是最好的实现:
public class ImageServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
private String imgPath;
public ImageServlet(String picPath){
super();
imgPath = picPath;
}
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
ServletContext cntx= req.getServletContext();
// Get the absolute path of the image
// retrieve mimeType dynamically
String filename = imgPath+ "/testpicture2.jpg";
String mime = cntx.getMimeType(imgPath+ "/testpicture2.jpg");
if (mime == null) {
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
resp.setContentType(mime);
File file = new File(filename);
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[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
out.close();
in.close();
}
我仍然在想,如果不使用这个变通方法(这是在另一个答案中建议的),我如何能够使用绝对路径获取本地资源,因此如果您仍然能够帮助我,那就太好了 根据javadoc:
“如果HttpContext参数为null,则使用默认HttpContext(请参阅createDefaultHttpContext())”
DefaultHttpContext的getResource方法的行为如下:
此方法调用上下文包的bundle.getResource方法,并返回>适当的URL以访问资源
如果要使用文件系统资源,必须实现自己的HttpContext,并根据javadoc实现getResource方法:
“如果HttpContext参数为null,则使用默认HttpContext(请参阅createDefaultHttpContext())”
DefaultHttpContext的getResource方法的行为如下:
此方法调用上下文包的bundle.getResource方法,并返回>适当的URL以访问资源
如果要使用文件系统资源,必须实现自己的HttpContext并实现方法getResource,根据registerResources的javadoc,应接受unix语法中的绝对路径。httpService.registerResources(“/testServer/pictures”,“C:/mypath/config”,null);这返回404,在其他资源返回UNAUTHORIZED的情况下,这意味着图片资源没有被创建-我想。编辑:唯一可能出错的是文件夹路径,但我已经在使用它在同一应用程序中读/写文件。根据registerResources的javadoc,应该接受unix语法中的绝对路径。httpService.registerResources(“/testServer/pictures”,“C:/mypath/config”,null);这返回404,在其他资源返回UNAUTHORIZED的情况下,这意味着图片资源没有被创建-我想。编辑:唯一可能出错的是文件夹路径,但我已经在同一个应用程序中使用它来读/写文件。我已经这样做了,最后它的行为类似于DefaultHttpContext,在我的情况下,它不起作用。你能发布你的实现吗?我已经这样做了,最后它的行为类似于DefaultHttpContext,在我的情况下,这不起作用。你能发布你的实现吗?