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

📄 filetransfer.java.svn-base

📁 类似QQ的功能
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************* Jimm - Mobile Messaging - J2ME ICQ clone Copyright (C) 2003-05  Jimm Project This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. ******************************************************************************** File: src/jimm/FileTransfer.java Version: ###VERSION###  Date: ###DATE### Author(s): Andreas Rossbacher, Dmitry Tunin, Denis Artyomov *******************************************************************************///#sijapp cond.if (target!="DEFAULT")&(modules_FILES="true")#package jimm;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.microedition.io.*;import javax.microedition.media.*;import javax.microedition.lcdui.*;//#sijapp cond.if target isnot "MOTOROLA"#import javax.microedition.media.control.VideoControl;//#sijapp cond.end#import DrawControls.*;import jimm.comm.Icq;import jimm.comm.FileTransferMessage;import jimm.comm.Message;import jimm.comm.PlainMessage;import jimm.comm.SendMessageAction;import jimm.comm.Util;import jimm.util.ResourceBundle;public class FileTransfer implements CommandListener, Runnable{	private static final int MODE_CHECK_FILE_LEN   = 10001;	private static final int MODE_SHOW_DESC_FORM   = 10002;	private static final int MODE_SEND_THROUGH_WEB = 10003;	private static final int MODE_BACK_TO_MENU     = 10004;		private static final int WEB_ASK_RESULT_YES = 20000;	private static final int WEB_ASK_RESULT_NO  = 20001;		private int curMode;		// Type of filetrasfer	public static final int FT_TYPE_FILE_BY_NAME = 1;	//#sijapp cond.if target isnot "MOTOROLA" #	public static final int FT_TYPE_CAMERA_SNAPSHOT = 2;	//#sijapp cond.end #	// Viewfinder	//#sijapp cond.if target isnot "MOTOROLA" #	private ViewFinder vf;	//#sijapp cond.end #	// Form for entering the name and description	private Form name_Desc;	// File data	private InputStream fis;	TextList tlWebAsk;	// File path and description TextField	private TextField fileNameField;	private TextField descriptionField;	// Type and ContactItem	private int type;		private Alert alert;	private ContactItem cItem;		private int fsize;	private String fileName;	private String shortFileName;	private String exceptionText;	private String lastWebLink;	private String description;		private FileSystem2 fileSystem;	// Commands	private Command backCommand = new Command(ResourceBundle.getString("back"),			Jimm.cmdBack, 2);	private Command okCommand = new Command(ResourceBundle.getString("ok"),			Command.OK, 1);	// Constructor	public FileTransfer(int ftType, ContactItem _cItem)	{		type = ftType;		cItem = _cItem;	}	// Return the cItem belonging to this FileTransfer	public ContactItem getCItem()	{		return (this.cItem);	}	// Set the file data	public void setData(InputStream is, int size)	{		fis = is;		fsize = size;	}	// Start the file transfer procedure depening on the ft type	public void startFT()	{		// Ask user about web file transfer		if (Options.getBoolean(Options.OPTION_ASK_FOR_WEB_FT) 		   && (Options.getInt(Options.OPTION_FT_MODE) == Options.FS_MODE_WEB))		{			tlWebAsk = new TextList(ResourceBundle.getString("ft_caption"));			JimmUI.setColorScheme(tlWebAsk, true, -1, true);						tlWebAsk.addBigText(ResourceBundle.getString("ft_web_ask"), tlWebAsk.getTextColor(), Font.STYLE_PLAIN, -1);			tlWebAsk.doCRLF(-1);			tlWebAsk.doCRLF(-1);			tlWebAsk.addBigText(ResourceBundle.getString("ft_web_yes"), tlWebAsk.getTextColor(), Font.STYLE_BOLD, WEB_ASK_RESULT_YES);			tlWebAsk.doCRLF(1);			tlWebAsk.addBigText(ResourceBundle.getString("ft_web_no"), tlWebAsk.getTextColor(), Font.STYLE_BOLD, WEB_ASK_RESULT_NO);			tlWebAsk.doCRLF(2);			tlWebAsk.selectTextByIndex(WEB_ASK_RESULT_YES);			tlWebAsk.addCommandEx(JimmUI.cmdSelect, VirtualList.MENU_TYPE_RIGHT_BAR);			tlWebAsk.setCommandListener(this);			tlWebAsk.activate(Jimm.display);			return;		}		else startFtInternal();	}		private void startFtInternal()	{		//#sijapp cond.if target isnot "MOTOROLA" #		if (type == FileTransfer.FT_TYPE_CAMERA_SNAPSHOT)		{			if (System.getProperty("video.snapshot.encodings") == null)				JimmException.handleException(new JimmException(185, 0, true));			else			{				vf = new ViewFinder();				Display.getDisplay(Jimm.jimm).setCurrent(vf);				Jimm.setBkltOn(true);				vf.start();			}		}		else if (type == FileTransfer.FT_TYPE_FILE_BY_NAME)		//#sijapp cond.end #		{			try			{				fileSystem = new FileSystem2();				fileSystem.browse(null, this, false);			} catch (Exception e)			{				//JimmException.handleException(e); TODO: do correct error handling			}		}	}		public void run() 	{		switch (curMode)		{		case MODE_CHECK_FILE_LEN:			try			{				fileSystem.openFile(fileName, Connector.READ);				fsize = (int)fileSystem.fileSize();				fis = fileSystem.openInputStream();				curMode = MODE_SHOW_DESC_FORM;				Jimm.display.callSerially(this);			} catch (Exception e) 			{				e.printStackTrace();			}			break;					case MODE_SHOW_DESC_FORM:			askForNameDesc(fileName, "");			break;					case MODE_SEND_THROUGH_WEB:			try			{				sendFileThroughWebThread();			}			catch (JimmException e)			{				exceptionText = e.getMessage();			}						curMode = MODE_BACK_TO_MENU;			Jimm.display.callSerially(this);						break;					case MODE_BACK_TO_MENU:			free();			if (exceptionText != null)			{				alert = new Alert(ResourceBundle.getString("ft_error"), exceptionText, null, AlertType.ERROR);				alert.setCommandListener(this);				alert.setTimeout(Alert.FOREVER);				Jimm.display.setCurrent(alert);				Jimm.setBkltOn(false);			}			else 			{				JimmUI.backToLastScreen();				JimmUI.setLastFileTransferLink(lastWebLink);			}			break;		}	}		private void sendFileThroughWebThread() throws JimmException	{		InputStream is;		OutputStream os;		HttpConnection sc;				exceptionText = null;				String host = "filetransfer.jimm.org";		String url = "http://"+host+"/__receive_file.php";				VirtualList.setMiniProgressBar(true);		VirtualList.setMpbPercent(0);		try		{			sc = (HttpConnection) Connector.open(url, Connector.READ_WRITE);			sc.setRequestMethod(HttpConnection.POST);						String boundary = "a9f843c9b8a736e53c40f598d434d283e4d9ff72";						sc.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);			os = sc.openOutputStream();						// Send post header			StringBuffer buffer2 = new StringBuffer();			buffer2.append("--").append(boundary).append("\r\n");			buffer2.append("Content-Disposition: form-data; name=\"jimmfile\"; filename=\"").append(shortFileName).append("\"\r\n");			buffer2.append("Content-Type: application/octet-stream\r\n");			buffer2.append("Content-Transfer-Encoding: binary\r\n");			buffer2.append("\r\n");			os.write(Util.stringToByteArray(buffer2.toString(), true));			// Send file data and show progress			byte[] buffer = new byte[1024];			int counter = fsize;			int read;			int percent;			do 			{				read = fis.read(buffer);				os.write(buffer, 0, read);				counter -= read;				if (fsize != 0)				{					percent = 100*(fsize-counter)/fsize;					VirtualList.setMpbPercent(percent);//					SplashCanvas.setProgress(percent);//					SplashCanvas.setMessage(ResourceBundle.getString("ft_transfer")+" "+percent+"% / "+fsize/1024+"KB");				}			} while (counter > 0);//			VirtualList.setMiniProgressBar(false);//			VirtualList.setMpbPercent(0);						// Send end of header			StringBuffer buffer3 = new StringBuffer();			buffer3.append("\r\n--").append(boundary).append("--\r\n");			os.write(Util.stringToByteArray(buffer3.toString(), true));			os.flush();			int respCode = sc.getResponseCode();			if (respCode != HttpConnection.HTTP_OK) throw new JimmException(194, respCode);						// Read response			is = sc.openInputStream();						StringBuffer response = new StringBuffer();			for (;;)			{				read = is.read();				if (read == -1) break; 				response.append((char)(read & 0xFF));			}						String respString = response.toString();						System.out.println(respString);						int dataPos = respString.indexOf("http://");			if (dataPos == -1) throw new JimmException(195, 0);						respString = Util.replaceStr(respString, "\r\n", "");			respString = Util.replaceStr(respString, "\r", "");			respString = Util.replaceStr(respString, "\n", "");						System.out.println(respString);			// Close all http connection headers 			os.close();			is.close();			sc.close();					// Send info about file			StringBuffer messText = new StringBuffer();			messText.append(ResourceBundle.getString("filename")).append(": ").append(shortFileName).append("\n");			messText.append(ResourceBundle.getString("size")).append(": ").append(fsize/1024).append("KB\n");			if (description != null && description.length() != 0)				messText.append(ResourceBundle.getString("description")).append(": ").append(description).append("\n");			messText.append(respString);					lastWebLink = messText.toString();			PlainMessage plainMsg = new PlainMessage(Options.getString(Options.OPTION_UIN), cItem, Message.MESSAGE_TYPE_NORM, Util.createCurrentDate(false), lastWebLink);			Icq.requestAction(new SendMessageAction(plainMsg));		}		catch (IOException e)		{			throw new JimmException(196, 0);		}	}		public static FileTransferMessage getFTM()	{		return ftm;	}		public static void clearFTM()	{		ftm = null; 	}		private static FileTransferMessage ftm;	// Init the ft	public void initFT(String filename, String description)	{		//#sijapp cond.if target isnot "MOTOROLA" #		this.vf = null;		//#sijapp cond.end #		        // Set the splash screen        SplashCanvas.setProgress(0);        SplashCanvas.setMessage(ResourceBundle.getString("init_ft"));        SplashCanvas.addCmd(SplashCanvas.cancelCommand);        SplashCanvas.setCmdListener(this);        SplashCanvas.show();		// Send the ft message		ftm = new FileTransferMessage(Options				.getString(Options.OPTION_UIN), this.cItem,				Message.MESSAGE_TYPE_EXTENDED, filename, description, fis,				fsize);		SendMessageAction act = new SendMessageAction(ftm);		try		{			Icq.requestAction(act);		} catch (JimmException e)		{			JimmException.handleException(e);			if (e.isCritical()) return;		}	}	public void askForNameDesc(String filename, String description)	{		name_Desc = new Form(ResourceBundle.getString("name_desc"));		this.fileNameField = new TextField(				ResourceBundle.getString("filename"), filename, 255,				TextField.ANY);		this.descriptionField = new TextField(ResourceBundle				.getString("description"), description, 255, TextField.ANY);		name_Desc.append(this.fileNameField);		name_Desc.append(this.descriptionField);		name_Desc.append(new StringItem(				ResourceBundle.getString("size") + ": ", String						.valueOf(fsize / 1024)						+ " kb"));		//#sijapp cond.if modules_TRAFFIC is "true" #		name_Desc.append(new StringItem(				ResourceBundle.getString("cost") + ": ",				Traffic.getString(((fsize / Options						.getInt(Options.OPTION_COST_PACKET_LENGTH)) + 1)						* Options.getInt(Options.OPTION_COST_PER_PACKET))						+ " " + Options.getString(Options.OPTION_CURRENCY)));		//#sijapp cond.end #		name_Desc.addCommand(this.backCommand);		name_Desc.addCommand(this.okCommand);		name_Desc.setCommandListener(this);		Jimm.display.setCurrent(name_Desc);		Jimm.setBkltOn(true);	}	// Command listener

⌨️ 快捷键说明

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