Zum Inhalt springen

Applet Java-Script Komunikation


U-- °LoneWolf°

Empfohlene Beiträge

Hi zusammen,

kaum ist das eine problem Erledigt kommt auch schon das nächste.

Folgendes Scenario ein Applet liest eine Datei z.B. ein Bild.

So weit kein Problem die eingelesenen daten werden an eine JavaScript funktion übergeben. Die daten werden im Javascript mit einem img Tag Dargsetellt.

Aus irgendweinem grund wird das encoding beim übertragen in Javascript zerstört ... hat jemand ne idee was man da machen kann?

anbei mal der code vom Applet und vom html

applet

import java.applet.Applet;

import java.applet.AppletContext;

import java.io.BufferedInputStream;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.net.MalformedURLException;

import java.net.URL;

import java.text.DateFormat;

import java.util.Date;

import java.net.*;

import java.io.*;


import netscape.javascript.JSObject;


/** An Applet to display the current time */


public class svnetFileManagement extends Applet implements Runnable {


	public java.lang.String ACTION=new String();//"getFilelist";//

	public java.lang.String RESULT="NULL";



	/** A Thread to run the timer */

	   protected Thread timerThread;

	   /** The date object */

	   Date date = new Date();

	   /** The date format */

	   protected DateFormat format = DateFormat.getTimeInstance();


	   /* Applet Lifestyle Methods */

	   public void start() {

		  this.initialise();

	      timerThread = new Thread(this, "Clock");

	      timerThread.start();

	   }


	   public void stop() {

	      if (timerThread == null)

	         return;

	      timerThread = null;

	   }

	   public void run() 

	   {

	      while (timerThread != null) 

	      {

	         repaint();   // request a redraw


	    	  try {

		    		if (!this.ACTION.isEmpty())

		    		{

		    			String sAction= this.ACTION;

		    			  if (this.ACTION.indexOf("getFilelist")==0)

		    			  {

		    				  this.RESULT = this.getFilelist();

		    			  }

		    			  else if(this.ACTION.indexOf("addFilelist")==0)

		    			  {

		    				  this.ACTION=this.ACTION.replace("addFilelist(\"", "");

		    				  this.ACTION=this.ACTION.replace("\")", "");

		    				  if(this.addFilelist(this.ACTION))

		    				  {

		    					  this.RESULT="SUCCESS";

		    				  }

		    			  }

		    			  else if(this.ACTION.indexOf("getFile")==0)

		    			  {

		    				  this.ACTION=this.ACTION.replace("getFile(\"", "");

		    				  this.ACTION=this.ACTION.replace("\")", "");

		    				  //System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>");

		    				  this.RESULT=this.getFile(this.ACTION);

		    				  //java.lang.String  test = new java.lang.String() 

		    				  System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>");

		    				  this.saveFile("testfile.jpg", this.RESULT);

		    				  System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>");

		    			  }

		    			  else if(this.ACTION.indexOf("saveFile")==0)

		    			  {

		    				  this.ACTION=this.ACTION.replace("saveFile(\"", "");

		    				  this.ACTION=this.ACTION.replace("\")", "");


		    				  if(this.saveFile(this.ACTION.split("\",\"")[0],this.ACTION.split("\",\"")[1]))

		    				  {

		    					  this.RESULT="SUCCESS";

		    				  }

		    				  else{

		    					  this.RESULT="ERROR";

		    				  }

		    			  }

		    			  else

		    			  {

		    				  this.RESULT="UNDEFINED_FUNCTION";

		    			  }


		    			  //System.out.println ( "Time's up!" ) ;

		    			  this.ACTION=new String();


		    			 try {


		    				 // JSObject test= new JSObject();

		    				  JSObject win = JSObject.getWindow(this);


		    			      JSObject doc = (JSObject) win.getMember("document");


		    			      //JSObject loc = (JSObject) doc.getMember("location");


		    			     // String s = (String) loc.getMember("href");  // document.location.href

		    			      Object[] obj = new Object[1];


		    			      obj[0]=sAction+"|<=>|" + this.RESULT ;

		    			     // obj[1]=this.RESULT ;

		    			     // System.out.println(sAction+"|<=>|" + this.RESULT );



		    			      win.call("RESULT", obj);	



		    			     // win.setMember("document", (Object)"arg1");

		    			      //win.notify()

		    			      // Call f() in HTML page


		    			     /* 

		    			      * möglichkeit miz GET Parametern

		    			      * URL url=null;

		    			      url=new URL("http://www.google.de/webhp?");

		    			      //q=

		    			      HttpURLConnection connection=(HttpURLConnection)url.openConnection();

		    			    	  connection.setRequestMethod("POST");

		    			    	  connection.setDoOutput(true);

		    			    	  PrintWriter out = new

		    			    	  PrintWriter(

		    			    	  connection.getOutputStream() );

		    			    	  out.println("p=applet\r\n");

		    			    	  out.close();


		    			    	  // there is no point attempting to show

		    			    	  // a document if the URL was malformed..

		    			    	  getAppletContext().showDocument(url);*/


		    			      //win.call("RESULT('test')", null);		  	     // Call f() in HTML page

		    			     //S win.call("javascript:RESULT('"+sAction+"|<=>|" + this.RESULT +"')", null);		  	     // Call f() in HTML page


		    			      /*String location = "http://localhost:800/ITSG/applet/test.php";

		    			      URL testServlet = new URL( location );

		    			      URLConnection servletConnection = testServlet.openConnection();


		    			      // inform the connection that we will send output and accept input

		    			      servletConnection.setDoInput(true);

		    			      servletConnection.setDoOutput(true);


		    			      // Don't use a cached version of URL connection.

		    			      servletConnection.setUseCaches (false);

		    			      servletConnection.setDefaultUseCaches (false);


		    			      // Specify the content type that we will send binary data

		    			      servletConnection.setRequestProperty("Content-Type", "<insert favorite mime type>");

		    			      URL url=new URL(location);

		    			      getAppletContext().showDocument(url);*/

		    			       }

		    			     catch (Exception me) { 

		    			    	 System.out.println (me.getMessage());

		    			     }

		    		}

		    		  timerThread.sleep(1000);

	    	  } catch (InterruptedException e){ /* do nothing*/ }

	      }

	   }


	   /** Display the time. */

	  /* public void paint(Graphics g) {

	      date.setTime(System.currentTimeMillis());

	      g.drawString(format.format(date), 2, 10);

	   }*/


	private File syspath ;

	private File tanfilelist;

	private File tanpath;

	private void initialise()

	{

		this.syspath = new  File(System.getProperty("user.home","")+"\\svnet");

		this.tanfilelist = new  File(syspath.getPath()+"\\DATA");

		this.tanpath = new  File(syspath.getPath()+"\\trandata");

		try {

			if (!this.syspath.exists())

	   		{

				this.syspath.mkdir();

	   		}

			if(!this.tanfilelist.exists())

			{

				try {

					this.tanfilelist.createNewFile();

				} catch (IOException e) {

					// TODO Auto-generated catch block

					e.printStackTrace();

				}

			}

			if (!this.tanpath.exists())

	   		{

				this.tanpath.mkdir();

	   		}

		} catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		}

		//getFilelist();

		//System.out.println(this.DATEN);

		//System.out.println(getFilelist());

	}

	private java.lang.String getFile(java.lang.String Path)

	{	

		File file = new File(Path);

		if (file.exists())

		{

			int arrlen = 10000;

			byte[] infile = new byte[arrlen];

			try {

				FileInputStream fis = new FileInputStream(file); 

				BufferedInputStream bis = new BufferedInputStream(fis);

				DataInputStream dis = new DataInputStream(bis);

				try {

					int filelength = dis.read(infile);

					String filestring = new String(infile, 0,filelength);//,"UTF-16"

					return filestring;

					//System.out.println("FILE CONTENT=" + filestring);

				} catch(IOException iox) {

					System.out.println("File read error...");

					iox.printStackTrace();

				}

			} catch (FileNotFoundException fnf) {

				System.out.println("File not found...");

				fnf.printStackTrace();

			}


		}

		return null;

	}


	private java.lang.String getFilelist()

	{

		int arrlen = 10000;

		byte[] infile = new byte[arrlen];

		try {

			java.io.FileInputStream fis = new java.io.FileInputStream(this.tanfilelist); 

			BufferedInputStream bis = new BufferedInputStream(fis);

			DataInputStream dis = new DataInputStream(bis);

			try {

				int filelength = dis.read(infile);

				String filestring = new String(infile, 0,filelength);

				return filestring;

			} catch(IOException iox) {

				System.out.println("File read error...");

				iox.printStackTrace();

			}

		} catch (FileNotFoundException fnf) {

			System.out.println("File not found...");

			fnf.printStackTrace();

		}

		return null;


	}

	private boolean saveFile(java.lang.String File, java.lang.String data)

	{

		try {

			//System.out.println(">>>>>>>a>>>>>>>>>>>>>>>>>>>>");

			//FileOutputStream fileOutStream = new FileOutputStream( this.tanpath+"\\"+File );

			//OutputStreamWriter fop = new OutputStreamWriter( fileOutStream, "UTF-8" );

			//

			FileOutputStream fop;

			fop = new FileOutputStream(this.tanpath+"\\"+File);

	        fop.write(data.getBytes());

	        fop.flush();

	        fop.close();

	        return true;

		} catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		}

		return false;

	}

	private boolean addFilelist(java.lang.String File)

	{

		try {

			FileOutputStream fop;

			StringBuilder data = new StringBuilder ();

			data.append(this.getFilelist());

			data.append("\r\n");

			data.append(File);

			fop = new FileOutputStream(this.tanfilelist);

	        fop.write((data.toString()).getBytes());

	        fop.flush();

	        fop.close();

	        return true;

		} catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		}

		return false;

	}

}



html
<?xml version="1.0" encoding="UTF-16"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>

<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-16" />

<SCRIPT>

function RESULT(sAction) {

	alert(sAction);


	sAction =unescape(sAction).replace(/\+/g, " ") 

	//var temp = new java.lang.String(sAction);

	//temp=new java.lang.String(DATA);

   if(sAction.indexOf("addFilelist")==0)

    {

    }

    else

    if (sAction.indexOf("getFilelist")==0)

    {

        document.getElementById('out').innerHTML='<table >'+sAction+'</table>';

    }

    else if(sAction.indexOf("getFile")==0)

    {  

    	//sAction= sAction.substr(sAction.indexOf("|<=>|"), sAction.length);

    	//document.getElementById('out').innerHTML ='<img src="data:image/jpg;base64,'+sAction+'"/>';

    	//var temp = document.svnetFileManagement.RESULT;

        document.svnetFileManagement.ACTION='saveFile("TETSBILD.jpg","'+document.svnetFileManagement.RESULT+'")';

       document.getElementById('out').innerHTML ='<form action="Result.php" name="outRESULT" id="outRESULT" method="POST" ><textarea name="DATA" >'+document.svnetFileManagement.RESULT+'</textarea></form>';

       document.getElementById('outRESULT').submit();

   }

   else if(sAction.indexOf("saveFile")==0)

   {

       alert(document.svnetFileManagement.RESULT);

   }



   //document.write(s);

   }

function ACTION()

{

    if(document.Eingabe.Text.value.indexOf("addFilelist")==0)

    {

        document.svnetFileManagement.ACTION=document.Eingabe.Text.value;

    }

    else

    if (document.Eingabe.Text.value.indexOf("getFilelist")==0)

    {

        document.svnetFileManagement.ACTION=document.Eingabe.Text.value;

    }

    else if(document.Eingabe.Text.value.indexOf("getFile")==0)

    {

        document.svnetFileManagement.ACTION=document.Eingabe.Text.value;

    }

    else if(document.Eingabe.Text.value.indexOf("saveFile")==0)

    {

        document.svnetFileManagement.ACTION=document.Eingabe.Text.value;

    }

}


</SCRIPT>


</head><body>

<div id="out" left="-500px">


</div>

<applet MAYSCRIPT="true" code="svnetFileManagement.class" archive="svnetFileManagement.jar" name="svnetFileManagement" width="200" height="25">

</applet><br>

<form >

<input type="text" name="Text">

<input type="button" value="ACTION"

onclick="ACTION()">

<input type="button" value="GET JOURNAL"

onclick="document.svnetFileManagement.ACTION='getFilelist'">

<input type="button" value="RESULT"

onclick="alert(document.svnetFileManagement.RESULT)">



</form>

</body></html>

wenn jemand ne idee hat wie ich die dateicodierung korrekt übertragen bekomme schon mal vielen dank

viele grüße

wolf

Link zu diesem Kommentar
Auf anderen Seiten teilen

  • 2 Wochen später...

Dein Kommentar

Du kannst jetzt schreiben und Dich später registrieren. Wenn Du ein Konto hast, melde Dich jetzt an, um unter Deinem Benutzernamen zu schreiben.

Gast
Auf dieses Thema antworten...

×   Du hast formatierten Text eingefügt.   Formatierung wiederherstellen

  Nur 75 Emojis sind erlaubt.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Editor leeren

×   Du kannst Bilder nicht direkt einfügen. Lade Bilder hoch oder lade sie von einer URL.

Fachinformatiker.de, 2024 by SE Internet Services

fidelogo_small.png

Schicke uns eine Nachricht!

Fachinformatiker.de ist die größte IT-Community
rund um Ausbildung, Job, Weiterbildung für IT-Fachkräfte.

Fachinformatiker.de App

Download on the App Store
Get it on Google Play

Kontakt

Hier werben?
Oder sende eine E-Mail an

Social media u. feeds

Jobboard für Fachinformatiker und IT-Fachkräfte

×
×
  • Neu erstellen...