做HarmonyOS应用开发的老铁们,有没有遇到过这样的场景:你正在开发一个离线文档阅读器,或者一个本地交互式H5页面,把HTML、CSS、JS文件都打包到rawfile目录下,心想这下用户没网也能用了。结果运行起来,页面是出来了,但CSS样式全没了,JS功能全挂了,图片显示全是破碎的占位符,控制台还报了一堆看不懂的跨域错误。
有兄弟会问,不对啊,我文件路径明明写对了,资源也确实在包里,怎么就是加载失败呢?实际上,Web组件加载本地资源的跨域问题,是HarmonyOS 6开发中最容易踩的坑之一。这篇文章就完整记录一下从问题现象到原因分析再到解决方案的全过程,帮你一次性搞定所有跨域难题。
一、问题背景:离线H5开发的"跨域噩梦"
1.1 两种典型的报错场景
场景一:本地HTML加载失败
需求:开发离线电子书阅读器,支持本地HTML+CSS+JS渲染
实现:将HTML文件和相关资源放入rawfile目录,使用Web组件加载
预期:完美显示带样式的离线页面
实际:HTML结构能显示,但CSS不生效,JS不执行,图片不加载
控制台报错:Access to script at 'xxx' from origin 'null' has been blocked by CORS policy
调试时间:平均浪费3-4小时
关键特征:页面只有骨架没有血肉,所有静态资源都被浏览器安全策略拦截。
场景二:file协议访问被拒
需求:实现本地文件预览功能,支持多种格式文件查看
实现:使用file://协议加载本地文件路径
问题:在HarmonyOS 6真机上完全无法加载,模拟器却正常
报错:Cross origin requests are only supported for protocol schemes
排查:协议支持、权限配置、系统版本差异
真相:HarmonyOS 6对本地文件访问的安全策略升级
关键特征:真机和模拟器表现不一致,新版本系统问题更突出。
1.2 官方文档的"隐藏细节"
根据HarmonyOS官方文档和实际开发经验分析,Web组件跨域问题有几个关键点容易忽略:
graph TD
A[Web组件本地资源加载失败] --> B[CSS样式不生效]
A --> C[JS脚本不执行]
A --> D[图片资源不显示]
A --> E[控制台报CORS错误]
B --> B1{原因分析}
B1 --> B2[同源策略限制]
B1 --> B3[协议不支持]
B1 --> B4[安全沙箱限制]
C --> C1{原因分析}
C1 --> C2[脚本执行被阻止]
C1 --> C3[DOM操作受限]
C1 --> C4[API调用失败]
D --> D1{原因分析}
D1 --> D2[图片路径解析错误]
D1 --> D3[跨域资源加载被拒]
D1 --> D4[MIME类型不匹配]
E --> E1{原因分析}
E1 --> E2[origin为null]
E1 --> E3[协议scheme不支持]
E1 --> E4[CORS头缺失]
B2 --> F[解决方案]
C2 --> F
D2 --> F
E2 --> F
F --> G[完整代码示例]
G --> H[避坑总结]
二、核心问题:四大跨域错误深度解析
2.1 坑点一:rawfile资源加载被CORS拦截
问题现象 :使用$rawfile('index.html')加载本地HTML文件,页面能显示但所有外部资源(CSS、JS、图片)都加载失败。
错误代码示例:
// 错误写法:直接使用rawfile路径
@Component
struct OfflineReaderPage {
@State webUrl: string = '';
aboutToAppear() {
// 错误1:直接使用rawfile路径
this.webUrl = '$rawfile('index.html')';
// 错误2:HTML中相对路径引用资源
// index.html内容:
// <link rel="stylesheet" href="./styles.css"> <!-- 会被CORS拦截 -->
// <script src="./app.js"></script> <!-- 会被CORS拦截 -->
// <img src="./images/cover.jpg"> <!-- 会被CORS拦截 -->
}
build() {
Column() {
Web({ src: this.webUrl, controller: this.webController })
.width('100%')
.height('100%')
.onPageEnd(() => {
console.info('页面加载完成');
// 但实际CSS、JS、图片都没加载
})
.onError((error) => {
console.error('Web组件错误:', error);
// 控制台报错:CORS policy blocked
})
}
.width('100%')
.height('100%')
}
}
控制台报错:
Access to script at 'file:///data/storage/el2/base/haps/entry/files/index/rawfile/styles.css'
from origin 'null' has been blocked by CORS policy:
Cross origin requests are only supported for protocol schemes: http, arkweb, data,
chrome-extension, chrome, https, chrome-untrusted.
问题分析:
-
同源策略限制:Web组件将rawfile资源视为不同源
-
origin为null:本地文件加载时origin被设置为null
-
协议不支持:file://协议在某些安全策略下被限制
2.2 坑点二:file协议在真机上完全失效
问题现象:在DevEco Studio模拟器上运行正常,但在HarmonyOS 6真机上完全无法加载本地文件。
错误代码示例:
// 错误写法:使用file协议访问本地文件
@Component
struct LocalFileViewer {
@State filePath: string = '';
private webController: WebController = new WebController();
aboutToAppear() {
// 获取本地文件路径
const context = getContext(this) as common.UIAbilityContext;
const filesDir = context.filesDir;
// 错误:使用file协议
this.filePath = `file://${filesDir}/documents/guide.html`;
// 模拟器上可能正常,但真机上会报错:
// Not allowed to load local resource: file://...
}
build() {
Column() {
Web({ src: this.filePath, controller: this.webController })
.width('100%')
.height('100%')
.onPageEnd(() => {
console.info('文件加载完成');
})
.onError((error) => {
console.error('加载失败:', error);
// 真机报错:ERR_ACCESS_DENIED
})
.fileAccess(true) // 即使开启文件访问,file协议仍可能被拒
}
}
}
问题分析:
-
安全策略升级:HarmonyOS 6加强了本地文件访问的安全限制
-
协议差异:模拟器和真机的Web内核实现可能不同
-
权限不足:需要额外的文件访问权限配置
2.3 坑点三:相对路径解析错误
问题现象:资源文件确实存在,路径也正确,但Web组件就是找不到。
错误代码示例:
// 错误写法:路径解析混乱
@Component
struct ResourceLoader {
@State webUrl: string = '';
async loadLocalHtml() {
try {
// 错误1:混合使用不同路径格式
const context = getContext(this) as common.UIAbilityContext;
// rawfile路径
const rawfilePath = 'entry/src/main/resources/rawfile/';
// 应用文件目录
const filesDir = context.filesDir;
// 尝试复制文件
const sourceUri = 'internal://app/rawfile/index.html';
const destPath = `${filesDir}/index.html`;
// 复制HTML文件
await this.copyFile(sourceUri, destPath);
// 错误2:使用错误的协议
this.webUrl = `file://${destPath}`;
// HTML中引用的资源路径问题更大:
// <link rel="stylesheet" href="styles.css">
// 这个相对路径会基于file://协议解析,可能指向错误位置
} catch (error) {
console.error('文件操作失败:', error);
}
}
async copyFile(source: string, dest: string): Promise<void> {
// 文件复制逻辑
// 但复制后,HTML中的相对路径引用会失效
}
build() {
Column() {
Web({ src: this.webUrl, controller: this.webController })
.width('100%')
.height('100%')
.onConsole((event) => {
// 控制台会输出404错误
console.error('资源加载失败:', event.message);
})
}
}
}
问题分析:
-
路径基准不一致:file://协议的基准路径与应用沙箱路径不同
-
相对路径解析 :
./styles.css在不同上下文中解析结果不同 -
文件权限:复制的文件可能没有正确的读取权限
2.4 坑点四:MIME类型不匹配
问题现象:资源能加载,但浏览器解析错误,CSS被当作文本,JS不执行。
错误代码示例:
// 错误写法:未正确设置MIME类型
@Component
struct MimeTypeIssue {
@State webUrl: string = '';
private webController: WebController = new WebController();
async setupLocalServer() {
// 尝试使用本地HTTP服务器
const server = new LocalHttpServer();
// 错误:未正确配置MIME类型
server.serveFile('/index.html', 'rawfile/index.html');
server.serveFile('/styles.css', 'rawfile/styles.css'); // 可能被当作text/plain
server.serveFile('/app.js', 'rawfile/app.js'); // 可能被当作application/octet-stream
this.webUrl = 'http://localhost:8080/index.html';
}
build() {
Column() {
Web({ src: this.webUrl, controller: this.webController })
.width('100%')
.height('100%')
.onPageEnd(() => {
console.info('页面加载完成');
// 但检查Network会发现:
// styles.css: Content-Type: text/plain (应该是text/css)
// app.js: Content-Type: application/octet-stream (应该是application/javascript)
})
}
}
}
class LocalHttpServer {
serveFile(path: string, filePath: string) {
// 简单实现,未设置正确的Content-Type
// 导致浏览器无法正确解析资源
}
}
问题分析:
-
Content-Type缺失:本地服务器未正确设置MIME类型
-
浏览器严格模式:现代浏览器对MIME类型检查更严格
-
资源解析失败:错误的MIME类型导致资源被忽略或解析错误
三、终极方案:四大坑点的完整解决方案
3.1 解决方案一:使用arkweb协议的正确写法
// Web组件本地资源加载服务(正确写法)
import web_webview from '@ohos.web.webview';
import fileio from '@ohos.fileio';
import fs from '@ohos.file.fs';
import common from '@ohos.app.ability.common';
class WebResourceService {
private static instance: WebResourceService;
private webController: web_webview.WebviewController | null = null;
private localServerPort: number = 0;
private resourceMap: Map<string, string> = new Map();
// 单例模式
static getInstance(): WebResourceService {
if (!WebResourceService.instance) {
WebResourceService.instance = new WebResourceService();
}
return WebResourceService.instance;
}
// 初始化Web组件
async initializeWebView(): Promise<web_webview.WebviewController> {
if (this.webController) {
return this.webController;
}
try {
// 1. 创建WebviewController
this.webController = new web_webview.WebviewController();
// 2. 配置Web设置
const webSetting = this.webController.getWebSetting();
// 启用JavaScript
webSetting.setJavaScriptAllowed(true);
// 启用文件访问(关键配置)
webSetting.setFileAccessEnabled(true);
// 启用跨域访问(关键配置)
webSetting.setCrossOriginEnabled(true);
// 启用本地资源访问
webSetting.setAllowFileAccessFromFileURLs(true);
webSetting.setAllowUniversalAccessFromFileURLs(true);
// 3. 设置WebClient
const webClient = this.webController.getWebClient();
// 处理资源加载请求
webClient.onInterceptRequest((request) => {
return this.handleResourceRequest(request);
});
// 处理控制台消息
webClient.onConsole((message) => {
console.info(`Web控制台: ${message.message}`);
});
console.info('Web组件初始化成功');
return this.webController;
} catch (error) {
console.error('Web组件初始化失败:', error);
throw error;
}
}
// 加载本地HTML文件(核心方法)
async loadLocalHtml(fileName: string): Promise<string> {
try {
// 方法1:使用arkweb协议(推荐)
const arkwebUrl = await this.getArkWebUrl(fileName);
// 方法2:使用data协议(备用方案)
// const dataUrl = await this.getDataUrl(fileName);
// 方法3:启动本地HTTP服务器(复杂场景)
// const httpUrl = await this.startLocalServer(fileName);
return arkwebUrl;
} catch (error) {
console.error('加载本地HTML失败:', error);
throw error;
}
}
// 方法1:使用arkweb协议(HarmonyOS专用)
private async getArkWebUrl(fileName: string): Promise<string> {
// arkweb协议格式:arkweb://[package]/[path]
const context = getContext() as common.UIAbilityContext;
const bundleName = context.abilityInfo.bundleName;
// 构建arkweb URL
const arkwebUrl = `arkweb://${bundleName}/entry/src/main/resources/rawfile/${fileName}`;
console.info(`arkweb URL: ${arkwebUrl}`);
// 验证文件是否存在
const fileExists = await this.checkRawFileExists(fileName);
if (!fileExists) {
throw new Error(`rawfile中不存在文件: ${fileName}`);
}
return arkwebUrl;
}
// 检查rawfile文件是否存在
private async checkRawFileExists(fileName: string): Promise<boolean> {
try {
const context = getContext() as common.UIAbilityContext;
const resourceManager = context.resourceManager;
// 尝试获取文件资源
const rawFileDescriptor = await resourceManager.getRawFileDescriptor(`entry/src/main/resources/rawfile/${fileName}`);
return rawFileDescriptor !== null;
} catch (error) {
console.warn(`检查文件失败: ${fileName}`, error);
return false;
}
}
// 方法2:使用data协议(Base64编码)
private async getDataUrl(fileName: string): Promise<string> {
try {
const context = getContext() as common.UIAbilityContext;
const resourceManager = context.resourceManager;
// 读取rawfile文件内容
const rawFileContent = await resourceManager.getRawFileContent(`entry/src/main/resources/rawfile/${fileName}`);
// 转换为Base64
const base64Content = this.arrayBufferToBase64(rawFileContent);
// 根据文件类型设置MIME
let mimeType = 'text/html';
if (fileName.endsWith('.html')) {
mimeType = 'text/html';
} else if (fileName.endsWith('.css')) {
mimeType = 'text/css';
} else if (fileName.endsWith('.js')) {
mimeType = 'application/javascript';
} else if (fileName.endsWith('.png')) {
mimeType = 'image/png';
} else if (fileName.endsWith('.jpg') || fileName.endsWith('.jpeg')) {
mimeType = 'image/jpeg';
}
// 构建data URL
const dataUrl = `data:${mimeType};base64,${base64Content}`;
console.info(`data URL生成成功,大小: ${base64Content.length} 字符`);
return dataUrl;
} catch (error) {
console.error('生成data URL失败:', error);
throw error;
}
}
// ArrayBuffer转Base64
private arrayBufferToBase64(buffer: ArrayBuffer): string {
let binary = '';
const bytes = new Uint8Array(buffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
// 处理资源请求拦截
private async handleResourceRequest(request: web_webview.InterceptRequest): Promise<web_webview.InterceptRequest> {
const url = request.url;
// 拦截本地资源请求
if (url.includes('rawfile/')) {
try {
// 提取文件名
const fileName = this.extractFileName(url);
// 检查是否已缓存
if (this.resourceMap.has(fileName)) {
const dataUrl = this.resourceMap.get(fileName);
if (dataUrl) {
// 返回data URL替换原请求
return {
url: dataUrl,
method: request.method,
headers: request.headers
};
}
}
// 生成data URL并缓存
const dataUrl = await this.getDataUrl(fileName);
this.resourceMap.set(fileName, dataUrl);
return {
url: dataUrl,
method: request.method,
headers: request.headers
};
} catch (error) {
console.error('处理资源请求失败:', error);
// 返回原请求,让Web组件处理
return request;
}
}
// 其他请求直接放行
return request;
}
// 从URL中提取文件名
private extractFileName(url: string): string {
// 匹配 rawfile/ 后面的文件名
const match = url.match(/rawfile\/(.+)$/);
if (match && match[1]) {
return match[1];
}
// 如果匹配失败,尝试从路径中提取
const parts = url.split('/');
return parts[parts.length - 1];
}
// 预加载资源(优化性能)
async preloadResources(fileList: string[]): Promise<void> {
console.info('开始预加载资源...');
const promises = fileList.map(async (fileName) => {
try {
const dataUrl = await this.getDataUrl(fileName);
this.resourceMap.set(fileName, dataUrl);
console.info(`预加载成功: ${fileName}`);
} catch (error) {
console.error(`预加载失败: ${fileName}`, error);
}
});
await Promise.all(promises);
console.info('资源预加载完成');
}
// 清理缓存
clearCache(): void {
this.resourceMap.clear();
console.info('资源缓存已清理');
}
}
// 使用示例
@Entry
@Component
struct OfflineWebViewer {
@State webUrl: string = 'about:blank';
@State isLoading: boolean = true;
@State errorMessage: string = '';
private webController: web_webview.WebviewController | null = null;
private webService: WebResourceService;
aboutToAppear() {
this.webService = WebResourceService.getInstance();
this.loadOfflineContent();
}
async loadOfflineContent() {
this.isLoading = true;
this.errorMessage = '';
try {
// 1. 初始化Web组件
this.webController = await this.webService.initializeWebView();
// 2. 预加载关键资源(优化体验)
await this.webService.preloadResources([
'styles.css',
'app.js',
'images/logo.png',
'images/cover.jpg'
]);
// 3. 加载主HTML文件
const mainUrl = await this.webService.loadLocalHtml('index.html');
this.webUrl = mainUrl;
console.info(`加载成功: ${this.webUrl}`);
} catch (error) {
this.errorMessage = `加载失败: ${error.message}`;
console.error('加载离线内容失败:', error);
// 备用方案:显示错误页面
this.webUrl = this.getErrorPageUrl(error.message);
} finally {
this.isLoading = false;
}
}
// 生成错误页面URL
private getErrorPageUrl(error: string): string {
const errorHtml = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>加载失败</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
height: 100vh;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
color: white;
}
.error-container {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 20px;
padding: 40px;
max-width: 500px;
text-align: center;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
h1 {
font-size: 48px;
margin: 0 0 20px 0;
}
p {
font-size: 18px;
line-height: 1.6;
margin: 0 0 30px 0;
opacity: 0.9;
}
.retry-btn {
background: white;
color: #667eea;
border: none;
padding: 15px 40px;
font-size: 18px;
border-radius: 50px;
cursor: pointer;
transition: transform 0.3s, box-shadow 0.3s;
}
.retry-btn:hover {
transform: translateY(-2px);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}
</style>
</head>
<body>
<div class="error-container">
<h1>😕</h1>
<h2>内容加载失败</h2>
<p>${this.escapeHtml(error)}</p>
<button class="retry-btn" onclick="window.location.reload()">重试</button>
</div>
<script>
// 错误页面也可以有交互
document.querySelector('.retry-btn').addEventListener('click', function() {
if (window.ohoswebview && ohoswebview.refresh) {
ohoswebview.refresh();
} else {
window.location.reload();
}
});
</script>
</body>
</html>
`;
return `data:text/html;base64,${btoa(unescape(encodeURIComponent(errorHtml)))}`;
}
// HTML转义
private escapeHtml(text: string): string {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
build() {
Column() {
// 加载状态
if (this.isLoading) {
Stack() {
Column() {
LoadingProgress()
.width(50)
.height(50)
.color(Color.Blue)
Text('正在加载离线内容...')
.fontSize(16)
.margin({ top: 20 })
.fontColor(Color.Gray)
}
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center)
.width('100%')
.height('100%')
}
.width('100%')
.height('100%')
} else if (this.errorMessage) {
// 错误状态
Column() {
Image($r('app.media.error_icon'))
.width(100)
.height(100)
.margin({ bottom: 20 })
Text('加载失败')
.fontSize(24)
.fontWeight(FontWeight.Bold)
.margin({ bottom: 10 })
.fontColor(Color.Red)
Text(this.errorMessage)
.fontSize(14)
.multilineTextAlign(TextAlign.Center)
.margin({ bottom: 30 })
.fontColor(Color.Gray)
.maxLines(3)
.textOverflow({ overflow: TextOverflow.Ellipsis })
Button('重试')
.onClick(() => {
this.loadOfflineContent();
})
.width(120)
}
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center)
.width('100%')
.height('100%')
} else {
// 正常显示Web组件
Web({ src: this.webUrl, controller: this.webController })
.width('100%')
.height('100%')
.onPageBegin(() => {
console.info('页面开始加载');
})
.onPageEnd(() => {
console.info('页面加载完成');
// 页面加载完成后可以执行一些操作
this.injectJavaScript();
})
.onProgressChange((progress) => {
console.info(`加载进度: ${progress}%`);
})
.onError((error) => {
console.error('Web组件错误:', error);
this.errorMessage = `页面加载错误: ${error.message}`;
})
.fileAccess(true) // 启用文件访问
.javaScriptAccess(true) // 启用JavaScript
.domStorageAccess(true) // 启用DOM存储
}
}
.width('100%')
.height('100%')
.backgroundColor(Color.White)
}
// 注入JavaScript与原生交互
private injectJavaScript(): void {
if (!this.webController) {
return;
}
// 注入JavaScript代码
const jsCode = `
// 与HarmonyOS原生交互
window.ohoswebview = {
// 刷新页面
refresh: function() {
window.location.reload();
},
// 返回上一页
goBack: function() {
if (window.history.length > 1) {
window.history.back();
}
},
// 显示Toast
showToast: function(message) {
// 调用HarmonyOS原生能力
if (window.ohos && ohos.postMessage) {
ohos.postMessage({
type: 'showToast',
data: message
});
}
},
// 获取设备信息
getDeviceInfo: function() {
return {
platform: 'HarmonyOS',
version: '6.0',
language: navigator.language,
userAgent: navigator.userAgent
};
}
};
// 监听来自原生的消息
window.addEventListener('message', function(event) {
if (event.data && event.data.type === 'fromNative') {
console.log('收到原生消息:', event.data);
// 处理消息
switch (event.data.action) {
case 'updateContent':
document.getElementById('content').innerHTML = event.data.content;
break;
case 'changeTheme':
document.body.classList.toggle('dark-mode');
break;
case 'getData':
// 返回数据给原生
const response = {
type: 'toNative',
data: { /* 数据 */ }
};
if (window.ohos && ohos.postMessage) {
ohos.postMessage(response);
}
break;
}
}
});
console.log('JavaScript注入完成');
`;
this.webController.runJavaScript(jsCode, (error, result) => {
if (error) {
console.error('注入JavaScript失败:', error);
} else {
console.info('JavaScript注入成功');
}
});
}
// 与Web页面通信
private sendMessageToWeb(message: any): void {
if (!this.webController) {
return;
}
const jsCode = `
if (window.dispatchEvent) {
const event = new CustomEvent('fromNative', {
detail: ${JSON.stringify(message)}
});
window.dispatchEvent(event);
}
// 或者调用全局函数
if (window.handleNativeMessage) {
window.handleNativeMessage(${JSON.stringify(message)});
}
`;
this.webController.runJavaScript(jsCode);
}
}
// 配置module.json5(关键权限配置)
/*
{
"module": {
"name": "entry",
"type": "entry",
"requestPermissions": [
{
"name": "ohos.permission.INTERNET",
"reason": "$string:internet_permission_reason",
"usedScene": {
"ability": ["EntryAbility"],
"when": "always"
}
},
{
"name": "ohos.permission.READ_MEDIA",
"reason": "$string:read_media_permission_reason",
"usedScene": {
"ability": ["EntryAbility"],
"when": "inuse"
}
},
{
"name": "ohos.permission.WRITE_MEDIA",
"reason": "$string:write_media_permission_reason",
"usedScene": {
"ability": ["EntryAbility"],
"when": "inuse"
}
}
],
"abilities": [
{
"name": "EntryAbility",
"srcEntry": "./ets/entryability/EntryAbility.ets",
"description": "$string:EntryAbility_desc",
"icon": "$media:icon",
"label": "$string:EntryAbility_label",
"startWindowIcon": "$media:icon",
"startWindowBackground": "$color:start_window_background",
"exported": true,
"skills": [
{
"actions": [
"action.system.home"
],
"entities": [
"entity.system.home"
]
}
]
}
]
}
}
*/
3.2 解决方案二:本地HTTP服务器方案(高级用法)
// 本地HTTP服务器实现(用于复杂离线应用)
import http from '@ohos.net.http';
import fileio from '@ohos.fileio';
import fs from '@ohos.file.fs';
import common from '@ohos.app.ability.common';
class LocalHttpServer {
private server: http.HttpServer | null = null;
private port: number = 8080;
private resourceBasePath: string = '';
private mimeTypes: Map<string, string> = new Map();
constructor() {
this.initMimeTypes();
}
// 初始化MIME类型映射
private initMimeTypes(): void {
this.mimeTypes.set('.html', 'text/html; charset=utf-8');
this.mimeTypes.set('.htm', 'text/html; charset=utf-8');
this.mimeTypes.set('.css', 'text/css; charset=utf-8');
this.mimeTypes.set('.js', 'application/javascript; charset=utf-8');
this.mimeTypes.set('.json', 'application/json; charset=utf-8');
this.mimeTypes.set('.png', 'image/png');
this.mimeTypes.set('.jpg', 'image/jpeg');
this.mimeTypes.set('.jpeg', 'image/jpeg');
this.mimeTypes.set('.gif', 'image/gif');
this.mimeTypes.set('.svg', 'image/svg+xml');
this.mimeTypes.set('.ico', 'image/x-icon');
this.mimeTypes.set('.ttf', 'font/ttf');
this.mimeTypes.set('.woff', 'font/woff');
this.mimeTypes.set('.woff2', 'font/woff2');
this.mimeTypes.set('.eot', 'application/vnd.ms-fontobject');
this.mimeTypes.set('.otf', 'font/otf');
}
// 启动服务器
async start(port: number = 8080): Promise<string> {
try {
this.port = port;
// 创建HTTP服务器
this.server = http.createHttpServer();
// 设置请求监听
this.server.on('request', async (request: http.HttpRequest, response: http.HttpResponse) => {
await this.handleRequest(request, response);
});
// 启动服务器
await this.server.listen(this.port, '127.0.0.1');
const url = `http://127.0.0.1:${this.port}`;
console.info(`本地HTTP服务器已启动: ${url}`);
return url;
} catch (error) {
console.error('启动HTTP服务器失败:', error);
throw error;
}
}
// 处理HTTP请求
private async handleRequest(request: http.HttpRequest, response: http.HttpResponse): Promise<void> {
try {
const url = request.url;
console.info(`收到请求: ${request.method} ${url}`);
// 设置CORS头(关键!)
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
response.setHeader('Access-Control-Allow-Headers', 'Content-Type');
response.setHeader('Access-Control-Max-Age', '86400');
// 处理预检请求
if (request.method === 'OPTIONS') {
response.writeHead(204);
response.end();
return;
}
// 解析请求路径
let filePath = this.getFilePath(url);
// 默认页面
if (filePath.endsWith('/')) {
filePath += 'index.html';
}
// 检查文件是否存在
const fileExists = await this.fileExists(filePath);
if (!fileExists) {
// 返回404
response.writeHead(404, { 'Content-Type': 'text/html; charset=utf-8' });
response.end('<h1>404 Not Found</h1>');
return;
}
// 读取文件内容
const fileContent = await this.readFile(filePath);
// 获取MIME类型
const mimeType = this.getMimeType(filePath);
// 设置响应头
const headers: Record<string, string> = {
'Content-Type': mimeType,
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
};
// 特殊处理:HTML文件添加CSP
if (mimeType.startsWith('text/html')) {
headers['Content-Security-Policy'] = "default-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self' data:;";
}
response.writeHead(200, headers);
response.end(fileContent);
} catch (error) {
console.error('处理请求失败:', error);
response.writeHead(500, { 'Content-Type': 'text/plain; charset=utf-8' });
response.end('Internal Server Error');
}
}
// 获取文件路径
private getFilePath(url: string): string {
// 移除查询参数
const path = url.split('?')[0];
// 解码URL编码
let decodedPath = decodeURIComponent(path);
// 防止目录遍历攻击
if (decodedPath.includes('..')) {
decodedPath = decodedPath.replace(/\.\./g, '');
}
// 构建完整路径
if (this.resourceBasePath) {
return `${this.resourceBasePath}/${decodedPath}`.replace(/\/+/g, '/');
}
return decodedPath;
}
// 检查文件是否存在
private async fileExists(path: string): Promise<boolean> {
try {
await fs.access(path);
return true;
} catch {
return false;
}
}
// 读取文件内容
private async readFile(path: string): Promise<Uint8Array> {
const file = await fs.open(path, fs.OpenMode.READ_ONLY);
try {
const stat = await fs.stat(path);
const buffer = new ArrayBuffer(stat.size);
await fs.read(file.fd, buffer);
return new Uint8Array(buffer);
} finally {
await fs.close(file.fd);
}
}
// 获取MIME类型
private getMimeType(filePath: string): string {
const ext = filePath.substring(filePath.lastIndexOf('.')).toLowerCase();
return this.mimeTypes.get(ext) || 'application/octet-stream';
}
// 设置资源基础路径
setResourceBasePath(path: string): void {
this.resourceBasePath = path;
}
// 停止服务器
async stop(): Promise<void> {
if (this.server) {
await this.server.close();
this.server = null;
console.info('HTTP服务器已停止');
}
}
// 获取服务器URL
getServerUrl(): string {
return `http://127.0.0.1:${this.port}`;
}
}
// 使用本地HTTP服务器的组件
@Component
struct LocalServerWebView {
@State serverUrl: string = '';
@State isServerRunning: boolean = false;
private webController: web_webview.WebviewController = new web_webview.WebviewController();
private httpServer: LocalHttpServer = new LocalHttpServer();
async aboutToAppear() {
await this.startLocalServer();
}
async startLocalServer() {
try {
// 设置资源路径(rawfile目录)
const context = getContext(this) as common.UIAbilityContext;
const rawfilePath = context.bundleCodeDir + '/entry/src/main/resources/rawfile/';
this.httpServer.setResourceBasePath(rawfilePath);
// 启动服务器
const baseUrl = await this.httpServer.start(8080);
this.serverUrl = `${baseUrl}/index.html`;
this.isServerRunning = true;
console.info(`服务器启动成功,访问地址: ${this.serverUrl}`);
} catch (error) {
console.error('启动本地服务器失败:', error);
this.isServerRunning = false;
// 回退到arkweb方案
await this.fallbackToArkWeb();
}
}
async fallbackToArkWeb() {
const webService = WebResourceService.getInstance();
this.serverUrl = await webService.loadLocalHtml('index.html');
}
aboutToDisappear() {
this.httpServer.stop();
}
build() {
Column() {
if (this.isServerRunning && this.serverUrl) {
Web({ src: this.serverUrl, controller: this.webController })
.width('100%')
.height('100%')
.onPageEnd(() => {
console.info('页面加载完成(通过本地服务器)');
})
.onError((error) => {
console.error('加载失败:', error);
// 尝试回退方案
this.fallbackToArkWeb();
})
} else {
// 服务器启动中或启动失败
Column() {
if (this.serverUrl) {
// 使用回退方案
Web({ src: this.serverUrl, controller: this.webController })
.width('100%')
.height('100%')
} else {
// 显示加载中
LoadingProgress()
.width(50)
.height(50)
Text('正在启动本地服务器...')
.fontSize(16)
.margin({ top: 20 })
}
}
.width('100%')
.height('100%')
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center)
}
}
.width('100%')
.height('100%')
}
}
四、避坑总结:Web组件本地资源加载最佳实践
4.1 方案选择指南
根据不同的应用场景,选择合适的本地资源加载方案:
| 场景 | 推荐方案 | 优点 | 缺点 | 适用性 |
|---|---|---|---|---|
| 简单离线页面 | arkweb协议 | 简单易用,无需额外配置 | 功能有限,调试困难 | ★★★★★ |
| 复杂H5应用 | 本地HTTP服务器 | 完整HTTP功能,支持AJAX | 实现复杂,需要启动服务 | ★★★☆☆ |
| 单文件展示 | data协议 | 无需文件系统访问 | 文件大小受限,性能差 | ★★☆☆☆ |
| 混合应用 | 资源预加载+arkweb | 平衡性能与复杂度 | 需要额外缓存管理 | ★★★★☆ |
4.2 关键配置检查清单
在遇到Web组件本地资源加载问题时,按以下清单逐一检查:
-
权限配置检查
-
module.json5中是否声明了INTERNET权限? -
是否开启了
fileAccess和javaScriptAccess? -
敏感权限是否动态申请?
-
-
Web设置检查
const webSetting = webController.getWebSetting(); webSetting.setFileAccessEnabled(true); // 必须开启 webSetting.setCrossOriginEnabled(true); // 跨域支持 webSetting.setAllowFileAccessFromFileURLs(true); // 文件URL访问 webSetting.setAllowUniversalAccessFromFileURLs(true); // 通用文件访问 -
路径格式检查
-
使用
arkweb://协议而非file:// -
确保rawfile路径正确:
arkweb://[bundleName]/entry/src/main/resources/rawfile/ -
HTML中资源使用相对路径时,确保基准路径正确
-
-
资源预加载优化
- 大型资源(图片、字体