導航:首頁 > 編程語言 > java生成二維碼亂碼

java生成二維碼亂碼

發布時間:2023-09-25 00:55:53

java生成圖片中文成了亂碼。

BufferedReader txtRead = new BufferedReader (new FileReader ("test.txt"));
讀取tests.txt的時候就是亂碼。

解決方法:

把上面那句改成這樣就ok了

BufferedReader txtRead=new BufferedReader(new InputStreamReader(new FileInputStream("test.txt"),"gbk"));

Ⅱ java轉碼後局部亂碼問題

從第一行正確可以看出,你的系統默認編碼是UTF-8;
第一行的意思是根據UTF-8解碼,然後再編碼
所以用內什麼解碼,就容需要用什麼編碼
String s = new String("中文測試局部亂碼問題2011中文測試上傳文件名亂碼問題.txt".getBytes(),"UTF-8");
System.out.println(s);
s = new String(s.getBytes("GBK"), "GBK");
System.out.println(s);
s = new String(s.getBytes("UTF-8"), "UTF-8");
System.out.println(s);

Ⅲ JAVA讀寫文件,如何避免中文亂碼

最近在做HTML靜態生成,需要從硬碟上把模版文件的內容讀出來。然後,替換相關標簽寫到指定的文件中。無論是讀寫,都遇到了中文亂碼問題。試過多種方法,發現下面一種可以避免中文亂碼。(無論讀取還是寫入一定要進行編碼轉換。)

1、JAVA讀取文件,避免中文亂碼。

/**
* 讀取文件內容
*
* @param filePathAndName
* String 如 c:\\1.txt 絕對路徑
* @return boolean
*/
public static String readFile(String filePathAndName) {
String fileContent = "";
try {
File f = new File(filePathAndName);
if(f.isFile()&&f.exists()){
InputStreamReader read = new InputStreamReader(new FileInputStream(f),"UTF-8");
BufferedReader reader=new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null) {
fileContent += line;
}
read.close();
}
} catch (Exception e) {
System.out.println("讀取文件內容操作出錯");
e.printStackTrace();
}
return fileContent;
}

2、JAVA寫入文件,避免中文亂碼。

public static void writeFile(String filePathAndName, String fileContent) {
try {
File f = new File(filePathAndName);
if (!f.exists()) {
f.createNewFile();
}
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f),"UTF-8");
BufferedWriter writer=new BufferedWriter(write);
//PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filePathAndName)));
//PrintWriter writer = new PrintWriter(new FileWriter(filePathAndName));
writer.write(fileContent);
writer.close();
} catch (Exception e) {
System.out.println("寫文件內容操作出錯");
e.printStackTrace();
}
}

我試過寫入的時候用

1、PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filePathAndName)));

2、PrintWriter writer = new PrintWriter(new FileWriter(filePathAndName));

都不行。

以上代碼經過運行可以避免中文亂碼,關鍵是要編碼轉換。

Ⅳ java中filewriter中文亂碼怎麼解決

要明白,用filewriter讀取文件採取的是平台默認編碼(視操作系統而定)。當要寫入的文本文件編碼和平台默認編碼不一致時,就會出現中文亂碼的情況。這時可以使用filewriter 的父類OutputStreamWriter來讀取。OutputStreamWriter允許用戶指定編碼方式,代碼為:
FileInputStream fis=new FileInputStream("文件路徑");
OutputStreamWriter osw=new OutputStreamWriter(fis,"文本文件的編碼方式(ANSI,UTF-8...)");
osw.write();
這樣寫入的編碼可以保證和源文本文件編碼一致,就不會出現亂碼了。。。

Ⅳ java生成二維碼在jsp中顯示亂碼

response.setContentType("image/jpeg");//設置相應類型,告訴瀏覽器輸出的內容為圖片
response.setHeader("Pragma", "No-cache");//設置響應頭信息,告訴瀏覽器不要緩存此內容
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expire", 0);

try {
ImageIO.write(image, "JPEG", response.getOutputStream());//將內存中的圖片通過流動形式輸出到客戶端,image是你的數據流,BufferedImage
} catch (Exception e) {
e.printStackTrace();
}

Ⅵ JAVA亂碼問題

對於Java,由於默認的編碼方式是UNICODE,所以用中文也易出問題,常見的解決是:

String s2 = new String(s1.getBytes(「ISO-8859-1」),」GBK」);

1、utf8解決JSP中文亂碼問題
一般說來在每個頁面的開始處,加入:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
%>

◆charset=UTF-8的作用是指定JSP向客戶端輸出的編碼方式為「UTF-8」;
◆pageEncoding="UTF-8",為了讓JSP引擎能正確地解碼含有中文字元的JSP頁面,這在LINUX中很有效;
◆request.setCharacterEncoding("UTF-8");是對請求進行了中文編碼。

有時,這樣仍不能解決問題,還需要這樣處理一下:

String msg = request.getParameter("message");
String str=new String(msg.getBytes("ISO-8859-1"),"UTF-8");
out.println(st);

Ⅶ java 生產二維碼報錯

你好,你可以參考我的這段代碼,記得導入Zxing1.6.jar即可:
public static void main(String[] args) {
String myCodeText = "http://www..com";
String filePath = "d:/testqr/myQR.png";
int size = 125;
String fileType = "png";
File myFile = new File(filePath);
try {
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = qrCodeWriter.encode(myCodeText,BarcodeFormat.QR_CODE, size, size, hintMap);
int CrunchifyWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(CrunchifyWidth, CrunchifyWidth,
BufferedImage.TYPE_INT_RGB);
image.createGraphics();

Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth);
graphics.setColor(Color.BLACK);

for (int i = 0; i < CrunchifyWidth; i++) {
for (int j = 0; j < CrunchifyWidth; j++) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i, j, 1, 1);
}
}
}
ImageIO.write(image, fileType, myFile);
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("\n\nYou have successfully created QR Code.");
}

記得採納哦

閱讀全文

與java生成二維碼亂碼相關的資料

熱點內容
公司文件打不開 瀏覽:267
LOL的設置文件在哪裡 瀏覽:254
線上開庭是什麼app 瀏覽:526
新中大軟體怎麼設置文件夾 瀏覽:807
git刪除文件夾 瀏覽:353
皇室戰爭怎麼升級競技場 瀏覽:447
雅虎統計工具 瀏覽:741
edgepdf文件分類 瀏覽:901
cad為什麼捕捉不到外部參照文件 瀏覽:935
重慶一共有多少個網站 瀏覽:34
k8s配置文件env創建失敗 瀏覽:197
編程序在電腦上叫做什麼 瀏覽:92
qq閱讀可賺 瀏覽:21
怎樣查找web儲存文件圖片 瀏覽:681
人口檔案資料庫包括什麼信息 瀏覽:709
手機有什麼好玩的星戰網路游戲 瀏覽:15
ps怎麼弄成轉曲文件 瀏覽:281
用數據線給手機傳文件 瀏覽:926
西門子jmp指令在編程軟體哪裡 瀏覽:335
win10加密共享文件夾不見了 瀏覽:412

友情鏈接