java中调用图片音频等文件的相对路径问题


import javax.swing.ImageIcon;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.applet.Applet;
import java.applet.AudioClip;

/**
*
* <p>Title: 获取资源</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2008</p>
* <p>Company: </p>
* @author landix
* @version 1.0
*/
public class ResourceManager {
//系统图片路径
private static String IMAGE_RESOURCE_PATH = "com/landix/oamq/client/images/";
//系统音频路径
private static String SOUND_RESOURCE_PATH = "com/landix/oamq/client/sound/";
//系统文件路径
private static String FILE_RESOURCE_PATH = "";

private ResourceManager() {
}

public static ImageIcon getIcon(String imageName) {
if (imageName == null) return(null);
ImageIcon icon = null;
try {
//通过类加载器加载图片资源,作为图标对象的参数
icon = new ImageIcon(ResourceManager.class.getClassLoader()
.getResource(IMAGE_RESOURCE_PATH + imageName));
} catch (Exception ex) {
System.out.println ("### Exception in getIcon() : \r\n" + IMAGE_RESOURCE_PATH + imageName + " " + ex.toString());
}

return icon;
}

public static Image getImage(String imageName) {
if (imageName == null) return(null);
Image image = null;
try {
//通过类加载器加载图片资源,作为图标对象的参数
image = Toolkit.getDefaultToolkit().getImage(ResourceManager.class.getClassLoader()
.getResource(IMAGE_RESOURCE_PATH + imageName));
} catch (Exception ex) {
System.out.println ("### Exception in getIcon() : \r\n" + IMAGE_RESOURCE_PATH + imageName + " " + ex.toString());
}

return image;
}

public static AudioClip getSound(String soundName) {
if (soundName == null) return(null);
AudioClip wowo = null ;
try {
//通过类加载器加载声音资源,作为声音对象的参数
wowo = Applet.newAudioClip(ResourceManager.class.getClassLoader()
.getResource(SOUND_RESOURCE_PATH + soundName));
} catch ( Exception ex ) {
System.out.println ("### Exception in getIcon() : \r\n" + SOUND_RESOURCE_PATH + soundName + " " + ex.toString());
}

return wowo;
}

public static File getFile(String fileName) {
if (fileName == null) return(null);
File file = null;
try {
//通过类加载器加载文件资源,作为文件对象的参数
file = new File(ResourceManager.class.getClassLoader()
.getResource(FILE_RESOURCE_PATH + fileName).toString());
} catch (Exception ex) {
System.out.println ("### Exception in getIcon() : \r\n" + FILE_RESOURCE_PATH + fileName + " " + ex.toString());
}

return file;
}

}
调用HTTP接口并传入WAV音频文件作为参数,可以使用Java的HTTP客户端库,如Apache HttpClient或OkHttp。 以下是使用Apache HttpClient发送HTTP POST请求并传输WAV音频文件的示例代码: ```java import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.File; import java.io.IOException; public class HttpPostExample { public static void main(String[] args) throws IOException { // 创建HTTP客户端 CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建HTTP POST请求 HttpPost httpPost = new HttpPost("http://example.com/api"); // 创建HTTP multipart请求体 MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addBinaryBody("audio_file", new File("/path/to/audio.wav"), ContentType.APPLICATION_OCTET_STREAM, "audio.wav"); // 设置HTTP请求体 httpPost.setEntity(builder.build()); // 执行HTTP POST请求 CloseableHttpResponse response = httpClient.execute(httpPost); try { // 处理HTTP响应 HttpEntity entity = response.getEntity(); String responseString = EntityUtils.toString(entity, "UTF-8"); System.out.println(responseString); } finally { response.close(); } } } ``` 在上述代码,我们创建了一个HTTP POST请求并设置了请求体为一个multipart请求体,其包含一个名为“audio_file”的文件参数,文件的内容来自于本地路径为“/path/to/audio.wav”的WAV音频文件。我们使用Apache HttpClient执行HTTP请求并处理HTTP响应。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值