1. 程式人生 > >JAVA Web專案中用OpenOffice+Swftools+Flexpaper實現線上預覽,txt檔案出現亂碼!

JAVA Web專案中用OpenOffice+Swftools+Flexpaper實現線上預覽,txt檔案出現亂碼!

在幼兒園管理系統中,實現線上預覽功能。當上傳word、ppt、excel、pdf的時候,不會出現亂碼;當上傳txt檔案的時候(編碼除UTF-8之外),會出現亂碼。當時有兩個方案。

方案一:

在上傳txt檔案的時候,判斷其編碼是否為UTF-8,如果不是,提示框:提示使用者上傳txt檔案必須為UTF-8格式。考慮到:有的使用者不會改txt的編碼。這個方案只好作罷。

方案二:

在上傳txt檔案的時候,判斷其編碼是否為UTF-8,如果不是,用程式碼轉換其txt編碼為UTF-8。程式碼如下:ChangeFileCode.java

package com.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class ChangeFileCode {
	// 讀取的檔案  
    private String fileIn;  
  
    // 讀取時檔案用的編碼  
    private String fileInEn;  
  
    // 寫出的檔案  
    private String fileOut;  
  
    // 寫出時檔案用的編碼  
    private String fileOutEn;  
  
    /**
     * 獲取原始檔的編碼
     * @param filePath 原始檔所在的絕對路徑
     * @return
     */
    public  String getFileEnCode(String filePath) {
		InputStream inputStream = null;
		String code = ""; 
		try {
			inputStream = new FileInputStream(filePath);  
	        byte[] head = new byte[3];  
	        inputStream.read(head);   
	        code = "gb2312";  
	        if (head[0] == -1 && head[1] == -2 )  
	            code = "UTF-16";  
	        if (head[0] == -2 && head[1] == -1 )  
	            code = "Unicode";  
	        if(head[0]==-17 && head[1]==-69 && head[2] ==-65)  
	            code = "UTF-8";  
	          
	        System.out.println(code);
		}catch(Exception e) {
			e.printStackTrace();
		} finally {
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return code;
	}
  
    public void setFileIn(String fileInPath, String fileInEncoding) {  
        this.setFileIn(fileInPath);  
        this.setFileInEn(fileInEncoding);  
    }  
  
    public void setFileOut(String fileOutPath, String fileOutEncoding) {  
        this.setFileOut(fileOutPath);  
        this.setFileOutEn(fileOutEncoding);  
    }  
      
    public void start() {     
        String str = this.read(fileIn,fileInEn);     
        this.write(fileOut, fileOutEn, str);     
    }     
      
    /** 
     * 讀檔案 
     *  
     * @param fileName 
     * @param encoding 
     */  
    private String read(String fileName, String encoding) {  
        try {  
            BufferedReader in = new BufferedReader(new InputStreamReader(  
                    new FileInputStream(fileName), encoding));  
  
            String string = "";  
            String str = "";  
            while ((str = in.readLine()) != null) {  
                string += str + "\n";  
            }  
            in.close();  
            return string;  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
        return "";  
    }  
  
    /** 
     * 寫檔案 
     *  
     * @param fileName 
     *            新的檔名 
     * @param encoding 
     *            寫出的檔案的編碼方式 
     * @param str 
     */  
    private void write(String fileName, String encoding, String str) {  
        try {  
            Writer out = new BufferedWriter(new OutputStreamWriter(  
                    new FileOutputStream(fileName), encoding));  
            out.write(str);  
            out.close();  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
    }  
  
    public String getFileIn() {  
        return fileIn;  
    }  
  
    public void setFileIn(String fileIn) {  
        this.fileIn = fileIn;  
    }  
  
    public String getFileInEn() {  
        return fileInEn;  
    }  
  
    public void setFileInEn(String fileInEn) {  
        this.fileInEn = fileInEn;  
    }  
  
    public String getFileOut() {  
        return fileOut;  
    }  
  
    public void setFileOut(String fileOut) {  
        this.fileOut = fileOut;  
    }  
  
    public String getFileOutEn() {  
        return fileOutEn;  
    }  
  
    public void setFileOutEn(String fileOutEn) {  
        this.fileOutEn = fileOutEn;  
    }  
  
    
    public static void main(String[] args) { 
    	String InputFilePath = "C:\\Users\\lenovo\\Desktop\\1\\";
    	String FileName = "2.txt";
    	
    	ChangeFileCode changeFileCode = new ChangeFileCode();  
        String path = InputFilePath+FileName;  
        File file = new File(path);  
        String fileCode = changeFileCode.getFileEnCode(path); 
        
        if(fileCode!=null && !"".equals(fileCode)) {
	        changeFileCode.setFileIn(file.getPath(), fileCode);//如果檔案編碼為ANSI用GBK來讀,如果是UTF-8用UTF-8來讀  
	        changeFileCode.setFileOut(file.getPath(), "UTF-8");//UTF-8則檔案編碼為UTF-8, 如果為GBK,編碼為ANSI  
	        changeFileCode.start();  
        }
    } 
}  


相關推薦

JAVA Web專案中用OpenOffice+Swftools+Flexpaper實現線上txt檔案出現亂碼

在幼兒園管理系統中,實現線上預覽功能。當上傳word、ppt、excel、pdf的時候,不會出現亂碼;當上傳txt檔案的時候(編碼除UTF-8之外),會出現亂碼。當時有兩個方案。 方案一: 在上傳txt檔案的時候,判斷其編碼是否為UTF-8,如果不是,提示框:提示使用者上傳

使用flexpaper實現線上功能時 遇到的flash快取問題的解放方案

本專案使用的是本地目錄對映為伺服器相對路徑,故預覽頁面時讀取的flash與專案不再同一碟符中,若更改flash檔案後,預覽還是隻顯示之前的舊檔案,一番搜尋後找到了解決方案,flexpaper 的官方demo中,有一個和java相關的demo,其中給出了一些方法,整理後使用如

java Web專案中的定時器實現

在Java的6.0版本中新增了concurrent包,這個包是用作執行緒同步的包,提供了很多工具類。 其中定時器就是其中之一。近期在做一個專案,在專案中要實現啟動Web服務的同時啟動一個任務,要求此任務為守護程序,定時呼叫一些任務操作程式碼。在網上搜索相關的實現,綜合考慮得

Java web專案01 停車管理系統實現新增使用者資訊功能(四)

(一)編寫一個新增使用者介面 <%@include file="/common/sub_header.jsp"%> <%@ page language="java" import

java 使用openoffice 轉換文件成.pdf實現線上效果

1. 下載 openoffice 地址     https://pan.baidu.com/s/1dfpoG6zlawoW1pqpDvBL0A 密碼: v4ej     如果上面的地址無法訪問請訪問這個地址:下載地址如下:http://www.openof

Java實現線上--openOffice實現

Java實現線上預覽–openOffice實現 簡介 之前有寫了poi實現線上預覽的文章,裡面也說到了使用openOffice也可以做到,這裡就詳細介紹一下。 我的實現邏輯有兩種: 一、利用jodconverter(基於OpenOffice服務)將檔案(.doc、.docx、.xls、.pp

centos6.5下安裝openoffice+jodconverter+swftool+flexpaper工具實現線上文件功能

作用:linux下文件伺服器上傳文件轉換成pdf文件,再由swftool工具轉換成swf檔案實現線上預覽 環境:OS   centos6.5           java環境 軟體包:Apac

Java web 專案讀取src或者tomcat下class目錄下的xml檔案或者properties檔案

//生成一個檔案物件: File file = new File(getClass().getClassLoader().getResource("test.xml").getPath());

Java實現線上openOffice實現

Java實現線上預覽–openOffice實現 簡介 之前有寫了poi實現線上預覽的文章,裡面也說到了使用openOffice也可以做到,這裡就詳細介紹一下。 我的實現邏輯有兩種: 一、利用jodconverter(基於OpenOffice服務)將檔案(

java實現線上--poi實現word、excel、ppt轉html

分享一下我的偶像大神的人工智慧教程!http://blog.csdn.net/jiangjunshow 也歡迎轉載我的文章,轉載請註明出處 https://blog.csdn.net/aabbyyz java實現線上預覽 - -之poi實現word、e

.net mvc使用FlexPaper外掛實現線上PDF,EXCEL,WORD的方法

  FlexPaper外掛可以實現在瀏覽器中線上預覽pdf,word,excel等。 在網上看到很多關於這個外掛實現預覽的技術,但是很難做到word和excel線上預覽。 pdf很好實現。   首先下載相關的外掛資訊,這裡不多說了。   其中這個外掛主要需要配合As

ionic3專案實現線上PDF檔案

這裡參考了大牛提供的預覽外掛完成自己需要實現的功能,ng2-pdf-viewer,該外掛不支援ionic3的懶載入,廢話少說,直接擼程式碼。 第一步,安裝 ng2-pdf-viewer npm install ng2-pdf-viewer --save 第二步,在專案中新建頁面

java實現線上--poi實現word excel ppt轉html

                        java實現線上

java實現線上office文件

本文采用openoffice+pdf2htmlEx和openoffice直接轉為html方式實現線上預覽 安裝,並作為服務在後臺啟動,後臺啟動命令:在openOffice的安裝目錄下的program資料夾下,windows命令:soffice -headless -ac

解決OpenOffice檔案轉換xlsx檔案出現com.sun.star.lang.IllegalArgumentException: URL seems to be an unsupported

OpenOffice在進行xlsx檔案預覽時出現發生如下錯誤: com.sun.star.lang.IllegalArgumentException: URL seems to be an unsupported one. at com.sun.star.lib

前端實現線上pdf、word、xls、ppt等檔案

1、前端實現pdf檔案線上預覽功能 方式一: 通過a標籤href屬性實現 pdf檔案理論上可以在瀏覽器直接開啟預覽但是需要開啟新頁面。在僅僅是預覽pdf檔案且UI要求不高的情況下可以直接通過a標籤href屬性實現預覽 <a href="文件地址"></

Word轉html實現線上

word轉html,可以同時支援doc和docx兩種格式,非常好用 開發工具:idea 專案管理工具:maven 不多說,直接擼程式碼 1、首先配置pom.xml檔案,具體配置如下 2、工具類的開發 /** * WORD轉HTML docx格式 * POI版本: 3.10-

Asp.net MVC 利用(aspose+pdfobject.js) 實現線上word、excel、ppt、pdf檔案

線上預覽word、excel、ppt利用aspose動態生成html 主要程式碼 private bool OfficeDocumentToHtml(string sourceDoc, string saveDoc) { bool result = false;

通過Aspose對Word,Excel檔案進行Pdf轉換,實現線上

解決思路:1.利用AsposeCells,AsposeWords相關Jar包提供的轉換功能,將Excel及Word型別文件轉換為Pdf檔案,並存於當前專案目錄下2.通過瀏覽器的iframe標籤功能,直接訪問應用下的相關Pdf檔案,目前主流瀏覽器均支援直接在頁面上瀏覽Pdf檔案

實現線上PDF的幾種解決方案

因客戶需要實現PDF的預覽處理,在網上找了一些PDF線上預覽的解決方案,有的用PDFJS的線上預覽方式,有的使用PDFObject的嵌入式顯示,有的通過轉換JPG/PNG方式實現間接顯示的方式,開始是想通過簡單的方式,能夠使用JS外掛實現預覽最好,可是線上預覽總是有一些不足,如不同瀏覽器的相容問題,甚至不同的