本地筆記本電腦windows10 64位
服務器SentOS 6.5 Final 64位
在使用pageoffice時候爲了測試方便,不修改代碼的前提下,可讀取配置文件中的路徑來解決
一、打開文件poCtrl.webOpen方法中的路徑
1.本地的配置文件如下:
配置文件內容:
主要是讀取path路徑,然後在根據實際情況進行拼接
//打開Word文檔
String path = Path.getFilePath(request) + "UploadFile" + File.separator +
"capital" + File.separator + "template.doc";
poCtrl.webOpen(path, OpenModeType.docSubmitForm, "wq");
此時打開的文檔位於
2.服務器配置文件如下:
配置文件內容:
此時打開的文檔位於:
----------------------------------------------------------------------------
二、保存文件,此時採用的路徑爲相對路徑,以項目爲主,可使用工具類中的方法
<%@ page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@ page import="com.util.Path"%>
<%@ page import="java.io.File"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.zhuozhengsoft.pageoffice.*"%>
<%
FileSaver fs = new FileSaver(request,response);
//獲取參數
long folder = ParamUtil.getLong(request, "folder");//獲取參數 需要新建文件夾名稱 123456
//路徑
String savepath = Path.getSaveFilePath(request) + "UploadFile" + File.separator + "flow" + File.separator + folder;
//先判斷文件夾是否存在
File f = new File(savepath);
if(!f.exists()){
System.out.println("文件夾NO存在");
f.mkdirs();//創建文件夾
}else{
System.out.println("文件夾存在");
}
savepath = savepath + File.separator + "repayTable.doc";
fs.saveToFile(savepath);
fs.close();
%>
保存的路徑爲UploadFile/flow/folder,其中folder是新建的文件夾,由於其是不存在的,要先創建,再將文件保存在該文件夾下
工具類:
package com.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
//本地的路徑和服務器路徑UploadFile
public class Path {
//pageoffice使用路徑
//服務器磁盤路徑--打開文檔使用
public static String getFilePath(HttpServletRequest request){
//路徑
String path = "";
String savePath = request.getSession().getServletContext().getRealPath("");
savePath = savePath.substring(0,savePath.lastIndexOf(File.separator));
String properties = savePath.substring(0,savePath.lastIndexOf(File.separator) + 1) + "config.properties";
InputStream is = null;
try {
is = new FileInputStream(properties);
Properties p = new Properties();
try {
p.load(is);
} catch (IOException e) {
e.printStackTrace();
}
path = p.getProperty("path").trim();
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return path;
}
//保存文檔使用
public static String getSaveFilePath(HttpServletRequest request){
//路徑
String path = "";
path = request.getSession().getServletContext().getRealPath("");
path = path.substring(0,path.lastIndexOf(File.separator));
path = path.substring(0,path.lastIndexOf(File.separator) + 1);
return path;
}
}
代碼經過本人測試,真實有效