當前位置:首頁 » 操作系統 » 解析java源碼

解析java源碼

發布時間: 2023-07-21 05:27:16

A. java 解析 eml的源代碼

//從EML文件得到MimeMessage對象
MimeMessagemessage=newMimeMessage(session,newFileInputStream(emlFile));

(Messagemessage)throwsException{
returnMimeUtility.decodeText(message.getSubject());
}

(Messagemessage)throwsException{
StringemailSender=null;
Address[]addresses=message.getFrom();
if(addresses==null||addresses.length<1){
("該郵件沒有發件人");
}

//獲得發件人
InternetAddressaddress=(InternetAddress)addresses[0];
StringsenderName=address.getPersonal();
if(senderName!=null){
senderName=MimeUtility.decodeText(senderName);
emailSender=senderName+"<"+address.getAddress()+">";
}else{
senderName=address.getAddress();
}
returnemailSender;
}

(Messagemessage,Message.RecipientTyperecipientType)throwsException{
StringBuilderbuilder=newStringBuilder();

Address[]addresses=null;
if(recipientType==null){
addresses=message.getAllRecipients();
}else{
addresses=message.getRecipients(recipientType);
}

if(addresses==null||addresses.length<1){
("該郵件沒有收件人");
}

for(Addressaddress:addresses){
InternetAddressiAddress=(InternetAddress)address;
builder.append(iAddress.toUnicodeString()).append(",");
}

returnbuilder.deleteCharAt(builder.length()-1).toString();
}

(Messagemessage,Stringpattern)throwsException{
StringsendDateString=null;

if(pattern==null||"".equals(pattern.trim())){
pattern="yyyy年MM月dd日EHH:mm";
}

DatesendDate=message.getSentDate();
sendDateString=newSimpleDateFormat(pattern).format(sendDate);

returnsendDateString;
}

(Partpart)throwsException{
booleanflag=false;
if(part!=null){
if(part.isMimeType("multipart/*")){
MimeMultipartmp=(MimeMultipart)part.getContent();
for(inti=0;i<mp.getCount();i++){
BodyPartbodyPart=mp.getBodyPart(i);
Stringdisposition=bodyPart.getDisposition();
if(disposition!=null&&(Part.ATTACHMENT.equalsIgnoreCase(disposition)
||Part.INLINE.equalsIgnoreCase(disposition))){
flag=true;
}elseif(bodyPart.isMimeType("multipart/*")){
flag=containsAttachment(bodyPart);
}else{
StringcontentType=bodyPart.getContentType();
if(contentType.indexOf("application")!=-1){
flag=true;
}

if(contentType.indexOf("name")!=-1){
flag=true;
}
}
if(flag)
break;
}
}elseif(part.isMimeType("message/rfc822")){
flag=containsAttachment((Part)part.getContent());
}
}

returnflag;
}

publicstaticbooleanisSeen(Messagemessage)throwsException{
if(message==null){
thrownewMessagingException("Messageisempty");
}
returnmessage.getFlags().contains(Flags.Flag.SEEN);
}

(Messagemessage)throwsException{
if(message==null){
thrownewMessagingException("Messageisempty");
}

booleanreplaySign=false;
String[]headers=message.getHeader("Disposition-Notification-To");
if(headers!=null&&headers.length>0){
replaySign=true;
}

returnreplaySign;
}

(Messagemessage)throwsException{
if(message==null){
thrownewMessagingException("Messageisempty");
}

Stringpriority="普通";

String[]headers=message.getHeader("X-Priority");
if(headers!=null&&headers.length>0){
StringmailPriority=headers[0];
if(mailPriority.indexOf("1")!=-1||mailPriority.indexOf("High")!=-1){
priority="緊急";
}elseif(mailPriority.indexOf("5")!=-1||mailPriority.indexOf("Low")!=-1){
priority="低";
}else{
priority="普通";//3或者Normal;
}

}

returnpriority;
}

(Partpart,StringBuildercontent)throwsException{
if(part==null){
thrownewMessagingException("Messagecontentisempty");
}
=part.getContentType().indexOf("name")>0;
if(part.isMimeType("text/*")&&containsTextInAttachment){
content.append(part.getContent().toString());
}elseif(part.isMimeType("message/rfc822")){
getMailTextContent((Part)part.getContent(),content);
}elseif(part.isMimeType("multipart/*")){
Multipartmp=(Multipart)part.getContent();
for(inti=0;i<mp.getCount();i++){
BodyPartbodyPart=mp.getBodyPart(i);
getMailTextContent(bodyPart,content);
}
}elseif(part.isMimeType("image/*")){
//TODOpart.getInputStream()獲得輸入流然後輸出到指定的目錄
}else{
//TODO其它類型的contentType,未做處理,直接輸出
content.append(part.getContent().toString());
}
}

(Partpart,StringdestDir)throwsException{
if(part==null){
thrownewMessagingException("partisempty");
}

//復雜的郵件包含多個郵件體
if(part.isMimeType("multipart/*")){
Multipartmp=(Multipart)part.getContent();
//遍歷每一個郵件體
for(inti=0;i<mp.getCount();i++){
BodyPartbodyPart=mp.getBodyPart(i);
//bodyPart也可能有多個郵件體組成
Stringdisposition=bodyPart.getDisposition();
if(disposition==null&&(Part.ATTACHMENT.equalsIgnoreCase(disposition)
||Part.INLINE.equalsIgnoreCase(disposition))){
InputStreamin=bodyPart.getInputStream();
saveFile(in,destDir,decodeText(bodyPart.getFileName()));
}elseif(bodyPart.isMimeType("multipart/*")){
saveAttachment(bodyPart,destDir);
}else{
StringcontentType=bodyPart.getContentType();
if(contentType.indexOf("name")!=-1||contentType.indexOf("application")!=-1){
saveFile(bodyPart.getInputStream(),destDir,decodeText(bodyPart.getFileName()));
}
}
}
}elseif(part.isMimeType("message/rfc822")){
saveAttachment((Part)part.getContent(),destDir);
}
}

publicstaticvoidsaveFile(InputStreamin,StringdestDir,StringfileName)throwsException{

FileOutputStreamout=newFileOutputStream(newFile(destDir+fileName));

byte[]buffer=newbyte[1024];
intlength=0;
while((length=in.read(buffer))!=-1){
out.write(buffer,0,length);
}

out.close();
in.close();
}

publicstaticStringdecodeText(StringencodedText)throwsException{
if(encodedText==null||"".equals(encodedText.trim())){
return"";
}else{
returnMimeUtility.decodeText(encodedText);
}
}

B. 如何使攝像頭掃描二維碼,然後解析二維碼 java源碼 zxing

import com.google.zxing.common.BitMatrix;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;

public final class MatrixToImageWriter {

private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;

private MatrixToImageWriter() {}

public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}

public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}

public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}

}

C. 如何看java源代碼

在eclipse中查看源文件用 「Ctrl+超找的源文件」 就可以了。 如我要查看String 我按住Ctrl,然後用滑鼠移動到String下面點擊就行了。
如果彈出一個窗口, 說「Source not found ... ... 」
在彈出的窗口上點擊那個按鈕「attach source」,會出來一個窗口讓你選擇jdk源碼包所在的位置,你選擇一下,比如在我機器上是「C:\Program Files\Java\jdk1.6.0_03\src.zip」,你類比著找一下,這樣就可以用「Ctrl+超找的類名「了。

D. java 源代碼注釋

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class GameTest extends JFrame implements ActionListener{
/*
* 新建一個主面板(這個類可能是自定義的,本程序和API中沒有)。
*/
MainPanel j=new MainPanel();
JButton jPreview;
JLabel label;
Container container;
JPanel panel;
/**
* 主函數
* @param args
*/
public static void main(String[] args) {
//運行程序
new GameTest();
}
/**
* 構造函數。
*
*/
public GameTest()
{
//新建一個標題為「拼圖」的窗口
JFrame fr =new JFrame("拼圖");
//獲取窗口容器。
container=fr.getContentPane();
//創建菜單條
JMenuBar jMenuBar=new JMenuBar();
//以下初始化菜單,並且設置快捷鍵和添加監聽器。
JMenu jMenuGame=new JMenu("游戲(G)");
jMenuGame.setMnemonic('g');

JMenuItem jMenuItemStart = new JMenuItem("開始(S)");
jMenuItemStart.setMnemonic('s');
jMenuItemStart.addActionListener(this);

JMenuItem jMenuItemExit=new JMenuItem("退出(E)");
jMenuItemExit.setMnemonic('e');
jMenuItemExit.addActionListener(this);

jMenuGame.add(jMenuItemStart);
jMenuGame.add(jMenuItemExit);
//初始化按鈕並設置快捷鍵和添加監聽器
JButton jChoice=new JButton("選圖(X)");
jChoice.setMnemonic('x');
jChoice.addActionListener(this);

jPreview=new JButton("預覽(P)");
jPreview.setMnemonic('p');
jPreview.addActionListener(this);
//將菜單和按鈕添加到菜單條中
jMenuBar.add(jMenuGame);
jMenuBar.add(jChoice);
jMenuBar.add(jPreview);
//將菜單條設為該窗口的主菜單
fr.setJMenuBar(jMenuBar);

//將主面板添加到該窗口的容器中。
container.add(j);
//設置大小
fr.setSize(315,360 );
fr.setVisible(true);
//設置默認關閉方式。
fr.setDefaultCloseOperation(3);

}
/**
* 事件處理函數。
*/
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="開始(S)")
{
j.Start();
}
if(e.getActionCommand()=="預覽(P)")
{
j.setVisible(false);
panel=new JPanel();
Icon icon=new ImageIcon("pictrue/pic"+"_"+MainPanel.pictureID+".jpg");
label=new JLabel(icon);
label.setBounds(300, 300, 0, 0);
panel.add(label);
panel.setSize(300, 300);
panel.setVisible(true);
this.container.add(panel);
jPreview.setText("返回(P)");
}
if(e.getActionCommand()=="返回(P)")
{
panel.setVisible(false);
j.setVisible(true);
j.repaint();
jPreview.setText("預覽(P)");
}
if(e.getActionCommand()=="退出(E)")
{
System.exit(0);
}

if(e.getActionCommand()=="選圖(X)")
{
//初始化選擇框,並提供選擇。
Choice pic = new Choice();
pic.add("七里香");
pic.add("依然范特西");
pic.add("八度空間");
pic.add("十一月的肖邦");
pic.add("魔傑座");
pic.add("葉惠美");
pic.add("我很忙");
int i=JOptionPane.showConfirmDialog(this, pic, "選擇圖片", JOptionPane.OK_CANCEL_OPTION);
if(i==JOptionPane.YES_OPTION)
{
//選擇圖片
MainPanel.pictureID=pic.getSelectedIndex()+1;
j.removeAll();
j.reLoadPicture();
j.repaint();
}
}
}

}

E. java編碼理解

<%@ page contentType= text/ charset=utf pageEncoding= GBK %>

jsp頁面(pageEncoding)——根據pageEncoding的設定讀取jsp——>翻譯成統一的UTF JAVA源碼(即 java)——由JAVAC的JAVA源碼至java byteCode的編譯——>

編譯成UTF encoding的二進制碼(即 class)——Tomcat(或其的application container)載入和執行階段二的來的JAVA二進制碼——>輸出contentType編碼給瀏覽器

頁面輸入的參數用pageEncoding來編碼

頁面的默認編碼是什麼?

ntentType的默認編碼是什麼?

編碼和解碼過程各種文件時什麼編碼

response setContentType( text/ charset=gb ) 是在頁面顯示時設置的字元格式request setCharacterEncoding( gb ) 是servlet接受請求後對請求中的字元進行設置字元格式 因為默認通過網路傳輸的內容都被進行了iso 編碼 如果想在後處理的時候不讓中文成亂碼 那就得對得到的內容進行gb 編碼

JSP pageEncoding和contentType屬性

JSP要經過兩次的 編碼 第一階段會用pageEncoding 第二階段會用utf 至utf 第三階段就是由Tomcat出來的網頁 用的是contentType

關於JSP頁面中的pageEncoding和contentType兩種屬性的區別

pageEncoding是jsp文件本身的編碼

contentType的charset是指伺服器發送給客戶端時的內容編碼

JSP要經過兩次的 編碼 第一階段會用pageEncoding 第二階段會用utf 至utf 第三階段就是由Tomcat出來的網頁 用的是contentType

第一階段是jsp編譯成 java 它會根據pageEncoding的設定讀取jsp 結果是由指定的編碼方案翻譯成統一的UTF JAVA源碼(即 java) 如果pageEncoding設定錯了 或沒有設定 出來的就是中文亂碼

第二階段是由JAVAC的JAVA源碼至java byteCode的編譯 不論JSP編寫時候用的是什麼編碼方案 經過這個階段的敏埋結果全部是UTF 的encoding的java源碼

JAVAC用UTF 的encoding讀取java源碼 編譯成UTF encoding的二進制碼(即 class) 這是JVM對常數字串在二進制碼(java encoding)內表達的規范

第三階段是Tomcat(或其的application container)載入和執行階段二的來的JAVA二進制碼 輸出的結果 也就是在客戶端見到的 這時隱藏在階段一和階段二的參數contentType就發揮了功效

contentType的設定

pageEncoding 和contentType的預設都是 ISO 而隨便設定了其中一個 另一個就跟著一樣了(TOMCAT 是如此) 但這不是絕對的 這要看各自JSPC的處理方式 而pageEncoding不等於contentType 更有利亞洲區的文字 CJKV系JSP網頁的開發和展示 (例pageEncoding=GB 不等於 contentType=utf )

jsp文件不像 java java在被編譯器讀入的時候默認採用的是操作系統所設定的locale所對應的編碼 一般我們不管是在段侍記事本還是在ue中寫代碼 如果沒有經過特別轉碼的話 寫出來的都是本地編碼格式的內容 所以編譯器採用的方法剛好可以讓虛擬機得到正確的資料

但是jsp文件不是這樣 它沒有這個默認轉碼過程 但是指定了pageEncoding就可以實現正確轉碼了

舉個例子

<%@ page contentType= text/ charset=utf %>大都會列印出亂碼 因為我輸橋燃螞入的 你好嗎 是gbk的 但是伺服器是否正確抓到 你好嗎 不得而知

但是如果更改為

lishixin/Article/program/Java/hx/201311/26477

熱點內容
安卓主板哪裡有賣 發布:2025-03-15 19:26:10 瀏覽:29
Q9源碼 發布:2025-03-15 19:24:21 瀏覽:175
芬蘭編程教育 發布:2025-03-15 18:59:46 瀏覽:426
網際網路的伺服器地址 發布:2025-03-15 18:53:01 瀏覽:892
手機實體店什麼配置好 發布:2025-03-15 18:32:35 瀏覽:168
攜帶型電腦的原始密碼是什麼 發布:2025-03-15 18:25:52 瀏覽:798
壓縮空間小 發布:2025-03-15 18:14:05 瀏覽:848
env的腳本 發布:2025-03-15 18:01:24 瀏覽:730
圖片上傳雲端 發布:2025-03-15 17:37:26 瀏覽:460
郵件伺服器ip池 發布:2025-03-15 17:31:51 瀏覽:398