对于javame.
既然getResourceAsStream()
是用于读取文件,getResourceAsStream()
输出流/写入文件是否相同?
注意:该文件位于项目的文件夹中,很快将打包到jar文件中.
我有一个java应用程序,我正在尝试加载将包含在jar中的文本文件.
当我这样做时getClass().getResource("/a/b/c/")
,它能够为该路径创建URL,我可以打印出来,一切都很好.
但是,如果我尝试getClass().getResource(/a/b/../")
,那么我会得到一个null
URL.
它似乎不喜欢..
在路径中.有谁看到我做错了什么?如果它有用,我可以发布更多代码.
我一直试图让getResource整个上午正常工作,但到目前为止,我必须让它工作的唯一方法是将res文件夹移动到bin文件夹,然后像这样使用getResource
URL url = MyClass.class.getResource("/res/gfx/entity/entity1.png");
Run Code Online (Sandbox Code Playgroud)
但是,我有没有办法让它如此res在bin文件夹之外?
我正在尝试使用以下代码从静态方法获取资源(image.png,与此代码在同一个包中):
import java.net.*;
public class StaticResource {
public static void main(String[] args) {
URL u = StaticResource.class.getClass().getResource("image.png");
System.out.println(u);
}
}
Run Code Online (Sandbox Code Playgroud)
输出只是'null'
我也尝试了StaticResource.class.getClass().getClassLoader().getResource("image.png");
,它抛出一个NullPointerException
我见过其他有效的解决方案,我做错了什么?
我使用以下方法从 WildFly 的 WAR 文件中获取资源:
this.getClass().getResource(relativePath)
Run Code Online (Sandbox Code Playgroud)
当应用程序部署为爆炸式 WAR 时,它会起作用。它曾经也适用于压缩的 WAR。昨天,我在 Eclipse 中对项目进行了清理和重建,但它刚刚停止工作。
当我检查资源根目录时:
logger.info(this.getClass().getResource("/").toExternalForm());
Run Code Online (Sandbox Code Playgroud)
我明白了:
file:/C:/JBoss/wildfly8.1.0.CR1/modules/system/layers/base/org/jboss/as/ejb3/main/timers/
Run Code Online (Sandbox Code Playgroud)
所以,难怪它不起作用。它可能与 JBoss 模块加载有关,但我不知道这是错误还是正常行为。
我在 StackOverflow 上发现了各种类似的问题,但没有适用的解决方案。建议之一是像这样使用 ServletContext:
@Resource
private WebServiceContext wsContext;
...
ServletContext servletContext = (ServletContext)this.wsContext.getMessageContext()
.get(MessageContext.SERVLET_CONTEXT);
servletContext.getResource(resourcePath);
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试以这种方式获取 MessageContext 时,我得到了一个 IllegalStateException。所以我基本上被卡住了。有任何想法吗?
我想知道为什么该方法getResource
不断返回null
,我有以下设置:
public static URL getResource(String path){
URL url = ResourceLoader.class.getResource(path);
if (Parameters.DEBUG){
System.out.println(path);
}
return url;
}
Run Code Online (Sandbox Code Playgroud)
我在Eclipse中的项目结构如下:
-- res
-- img
Run Code Online (Sandbox Code Playgroud)
path
我传递给的变量getResource
有值"/res/img"
或"/res/img/smile.png"
.然而,该方法保持返回null
并且url
未设置.我也按照这个问题的说明,通过运行配置将文件夹添加到项目的类路径中,仍然没有成功......有谁知道我做错了什么?
我正在尝试访问项目中的文件.但是getResource方法返回null.
这就是我的项目的样子:
Thread.currentThread().getContextClassLoader().getResource("assets/xxx.png"); //returns null
Run Code Online (Sandbox Code Playgroud)
以及eclipse工作区中的项目文件夹如何:
为什么?我想访问资产文件夹中的文件?
编辑 我创建了一个jar文件,这是jar的内容:
解决了
首先,我有很多图像文件,所以我想将它们整理到一个文件夹中.我把assets文件夹放在src目录中,最后我能够访问这些文件.
this.getClass().getClassLoader().getResource("assets/xxx.png");
Run Code Online (Sandbox Code Playgroud)
我想在TabView上添加图标,但由于无法getResources()
在自定义TabAdapter中使用而被卡住。我已经尝试使用getAplicationContext().getResources, getActivity.getResources()
,但是仍然看到该消息"cannot resolve method"
。有人可以帮我吗?
package com.glapps.mobile.codifynotes.Adapter;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import com.glapps.mobile.codifynotes.Activity.MainActivity;
import com.glapps.mobile.codifynotes.Fragment.CodifyFragment;
import com.glapps.mobile.codifynotes.Fragment.Configuracoes.ConfiguracoesFragment;
import com.glapps.mobile.codifynotes.Fragment.DecodifyFragment;
import com.glapps.mobile.codifynotes.R;
public class TabAdapter extends FragmentStatePagerAdapter {
private String[] tituloAba = {"CODIFICAR", "DECODIFICAR", "CONFIGURAÇÕES"};
private int[] imageResId = {
R.drawable.ic_action_lock_closed,
R.drawable.ic_action_lock_closed,
R.drawable.ic_action_settings_white
};
public TabAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position){
case 0: …
Run Code Online (Sandbox Code Playgroud) 我的 Spring Boot 应用程序出现以下错误:
There was an unexpected error (type=Internal Server Error, status=500).
inStream parameter is null
Run Code Online (Sandbox Code Playgroud)
该应用程序在 IntelliJ 中运行没有错误。打包后.jar
或.war
无法再找到我的属性文件。
Properties prop = new Properties();
prop.load(WebtvApplication.class.getClassLoader().getResourceAsStream("sender.properties"));
Run Code Online (Sandbox Code Playgroud)
这是我的目录和属性文件的屏幕截图:
我的 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>de.felix</groupId>
<artifactId>webtv</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>webtv</name>
<description>Demo project for Spring Boot</description>
<packaging>war</packaging>
<properties>
<java.version>11</java.version>
<start-class>de.felix.webtv.WebtvApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency> …
Run Code Online (Sandbox Code Playgroud) 这个问题在很多地方都有人提出,但也有很多细微的差别。(例如Java - getClassLoader().getResource() 让我发疯等。)我仍然无法让它工作。
这是一个代码片段:
String clipName = "Chook.wav";
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// URL url = classLoader.getResource(clipName);
URL url = new URL("file:///Users/chap/Documents/workspace/the1620/bin/ibm1620/" + clipName);
ais = AudioSystem.getAudioInputStream(url);
Run Code Online (Sandbox Code Playgroud)
这是有效的 - 请注意,我已经硬编码了包含剪辑文件的目录的路径,该文件就在那里,并且与我的 .class 文件位于同一目录中。唉,注释掉的代码只是返回 url 的空值。
大多数其他帖子似乎都涉及 getResourceAsStream()。我想我应该使用 getResource() 。这有区别吗?
它不可能这么难。有什么线索吗?