⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectpusher.java

📁 蓝牙 Obex 源码 J2me 初学者必须的东西
💻 JAVA
字号:
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.bluetooth.*;
import javax.obex.*;

public class ObjectPusher extends Thread{


	String connectionURL = null;
	Connection connection = null;
	File file = null;
	FileTransferPanel fileTransferPanel = null;

	public ObjectPusher(FileTransferPanel fileTransferPanel, File file, String connectionURL){

		this.file = file;
		this.connectionURL = connectionURL;
		this.fileTransferPanel = fileTransferPanel;

	}


	public void run(){

		try{
			connection = Connector.open(connectionURL);
			fileTransferPanel.updateStatus("Connection obtained");


	            ClientSession cs = (ClientSession)connection;
	            HeaderSet hs = cs.createHeaderSet();

	            cs.connect(hs);
			fileTransferPanel.updateStatus("OBEX session created");      


			InputStream is = new FileInputStream(file);
	            byte filebytes[] = new byte[is.available()];
	            is.read(filebytes);
	            is.close();


	            hs = cs.createHeaderSet();
	            hs.setHeader(HeaderSet.NAME, file.getName());
	            hs.setHeader(HeaderSet.TYPE, "audio/x-mp3");
 			hs.setHeader(HeaderSet.LENGTH, new Long(filebytes.length));

	            Operation putOperation = cs.put(hs);
			fileTransferPanel.updateStatus("Pushing file: " + file.getName());
			fileTransferPanel.updateStatus("Total file size: " + filebytes.length);

			OutputStream outputStream = putOperation.openOutputStream();
			outputStream.write(filebytes);
			fileTransferPanel.updateStatus("File push complete");

			outputStream.close();
	            putOperation.close();
			
	            cs.disconnect(null);

			connection.close();
		} catch (Exception e){
		}


	}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -