Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » Write Image uploaded by Upload Widget to File
Write Image uploaded by Upload Widget to File [message #501507] Wed, 02 December 2009 15:31 Go to next message
hannes is currently offline hannesFriend
Messages: 12
Registered: October 2009
Location: Germany
Junior Member
Hello,

im trying to write the FileInputStream of an uploaded jpeg to a file.

Im using the following code
        if(uploadItem.getContentType().equals("image/jpeg"));{

    		ImageRegistry registry = new ImageRegistry(Display.getCurrent());
    	    registry.put("image", ImageDescriptor.createFromImage(new Image(Display.getCurrent(), uploadItem.getFileInputStream())));
    	    Image image = registry.get("image");
    	    
    	    
        	byte[] imageData = image.getImageData().data;
        	try {
        		fos = new FileOutputStream(properties.getProperty("imagePath")+uploadItem.getFileName());
        		fos.write(imageData);
        		fos.close();
        	} catch (Exception e1) {
        		// TODO Auto-generated catch block
        		e1.printStackTrace();
        	}

        }



But this throws the following Exception
## total: 2395521
filename     : IMGP0847.JPG
path         : IMGP0847.JPG
content-type : image/jpeg
stream       : java.io.FileInputStream@4b12d9
2009-12-02 16:36:08.250::WARN:  ERROR:  /rap
org.eclipse.rwt.internal.resources.ResourceRegistrationException: Failed to register resource 'image-11362219'.
	at org.eclipse.rwt.internal.resources.ResourceManagerImpl.register(ResourceManagerImpl.java:239)
	at org.eclipse.rap.ui.internal.servlet.ResourceManagerFactory$ResourceManagerWrapper.register(ResourceManagerFactory.java:130)
	at org.eclipse.swt.internal.graphics.ResourceFactory.registerImage(ResourceFactory.java:346)
	at org.eclipse.swt.graphics.Image.init(Image.java:178)
	at org.eclipse.swt.graphics.Image.<init>(Image.java:121)
	at gui.apps.BannerUpload.handleUploadFinished(BannerUpload.java:128)
	at gui.apps.BannerUpload.access$1(BannerUpload.java:105)
	at gui.apps.BannerUpload$2.uploadFinished(BannerUpload.java:77)
	at org.eclipse.rwt.widgets.UploadEvent.dispatchToObserver(UploadEvent.java:92)
	at org.eclipse.rwt.internal.events.Event.processEvent(Event.java:44)
	at org.eclipse.swt.events.TypedEvent.processEvent(TypedEvent.java:129)
	at org.eclipse.rwt.widgets.internal.uploadkit.UploadLCA$1.run(UploadLCA.java:78)
	at org.eclipse.rwt.lifecycle.ProcessActionRunner.executeNext(ProcessActionRunner.java:57)
	at org.eclipse.rwt.internal.lifecycle.RWTLifeCycle.readAndDispatch(RWTLifeCycle.java:238)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:981)
	at org.eclipse.rwt.widgets.Upload.performUpload(Upload.java:251)
	at gui.apps.BannerUpload$1.widgetSelected(BannerUpload.java:67)
	at org.eclipse.swt.events.SelectionEvent.dispatchToObserver(SelectionEvent.java:194)
	at org.eclipse.rwt.internal.events.Event.processEvent(Event.java:44)
	at org.eclipse.swt.events.TypedEvent.processEvent(TypedEvent.java:129)
	at org.eclipse.swt.events.TypedEvent.executeNext(TypedEvent.java:169)
	at org.eclipse.rwt.internal.lifecycle.RWTLifeCycle.readAndDispatch(RWTLifeCycle.java:240)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:981)
	at gui.AppStarter.startApp(AppStarter.java:60)
	at gui.AdDeskNavBar$1.widgetSelected(AdDeskNavBar.java:50)
	at org.eclipse.swt.events.SelectionEvent.dispatchToObserver(SelectionEvent.java:194)
	at org.eclipse.rwt.internal.events.Event.processEvent(Event.java:44)
	at org.eclipse.swt.events.TypedEvent.processEvent(TypedEvent.java:129)
	at org.eclipse.swt.events.TypedEvent.executeNext(TypedEvent.java:169)
	at org.eclipse.rwt.internal.lifecycle.RWTLifeCycle.readAndDispatch(RWTLifeCycle.java:240)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:981)
	at gui.AdServerControl.createUI(AdServerControl.java:59)
	at org.eclipse.rwt.internal.lifecycle.EntryPointManager.createUI(EntryPointManager.java:92)
	at org.eclipse.rwt.internal.lifecycle.RWTLifeCycle.createUI(RWTLifeCycle.java:227)
	at org.eclipse.rwt.internal.lifecycle.RWTLifeCycle$UIThreadController.run(RWTLifeCycle.java:115)
	at java.lang.Thread.run(Unknown Source)
	at org.eclipse.rwt.internal.lifecycle.UIThread.run(UIThread.java:80)
Caused by: java.io.IOException: Read error
	at java.io.FileInputStream.readBytes(Native Method)
	at java.io.FileInputStream.read(Unknown Source)
	at java.io.BufferedInputStream.fill(Unknown Source)
	at java.io.BufferedInputStream.read1(Unknown Source)
	at java.io.BufferedInputStream.read(Unknown Source)
	at java.io.BufferedInputStream.fill(Unknown Source)
	at java.io.BufferedInputStream.read(Unknown Source)
	at org.eclipse.rwt.internal.resources.ResourceUtil.getAvailableCount(ResourceUtil.java:203)
	at org.eclipse.rwt.internal.resources.ResourceUtil.readBinary(ResourceUtil.java:187)
	at org.eclipse.rwt.internal.resources.ResourceManagerImpl.register(ResourceManagerImpl.java:234)
	... 36 more


Can anyone help me please ?

I also tried to write out the InputStream byte by byte, but this also dont work.

Regards,
hannes

[Updated on: Wed, 02 December 2009 15:42]

Report message to a moderator

Re: Write Image uploaded by Upload Widget to File [message #501698 is a reply to message #501507] Thu, 03 December 2009 14:09 Go to previous messageGo to next message
Ivan Furnadjiev is currently offline Ivan FurnadjievFriend
Messages: 2359
Registered: July 2009
Location: Sofia, Bulgaria
Senior Member
Hi hannes,

it's look like your image input stream can't be read. Check it if it is
already closed or not.

Best,
Ivan

On 12/2/2009 17:31, hannes wrote:
> Hello,
>
> im trying to write the FileInputStream of an uploaded jpeg to a file.
>
> Im using the following code
> if(uploadItem.getContentType().equals("image/jpeg"));{
>
> ImageRegistry registry = new
> ImageRegistry(Display.getCurrent());
> registry.put("image", ImageDescriptor.createFromImage(new
> Image(Display.getCurrent(), uploadItem.getFileInputStream())));
> Image image = registry.get("image");
> byte[] imageData =
> image.getImageData().data;
> try {
> fos = new
> FileOutputStream(properties.getProperty("imagePath")+uploadItem.getFileName());
>
> fos.write(imageData);
> fos.close();
> } catch (Exception e1) {
> // TODO Auto-generated catch block
> e1.printStackTrace();
> }
>
> }
>
>
>
> But this throws the following Exception
>
> ## total: 2395521
> filename : IMGP0847.JPG
> path : IMGP0847.JPG
> content-type : image/jpeg
> stream : java.io.FileInputStream@cb754f
> 2009-12-02 16:28:07.796::WARN: ERROR: /rap
> org.eclipse.rwt.internal.resources.ResourceRegistrationExcep tion:
> Failed to register resource 'image-16491778'.
> at
> org.eclipse.rwt.internal.resources.ResourceManagerImpl.regis ter(ResourceManagerImpl.java:239)
>
> at
> org.eclipse.rap.ui.internal.servlet.ResourceManagerFactory$R esourceManagerWrapper.register(ResourceManagerFactory.java:1 30)
>
> at
> org.eclipse.swt.internal.graphics.ResourceFactory.registerIm age(ResourceFactory.java:346)
>
> at org.eclipse.swt.graphics.Image.init(Image.java:178)
> at org.eclipse.swt.graphics.Image.<init>(Image.java:121)
>
>
> Can anyone help me please ?
>
> I also tried to write out the InputStream byte by byte, but this also
> dont work.
>
> Regards,
> hannes
Re: Write Image uploaded by Upload Widget to File [message #501910 is a reply to message #501507] Fri, 04 December 2009 12:42 Go to previous messageGo to next message
hannes is currently offline hannesFriend
Messages: 12
Registered: October 2009
Location: Germany
Junior Member
Yep i did close it .
These lines were executed before the other lines of my first post
        try {
            if (uploadItem.getFileInputStream() != null) {
            	
				uploadItem.getFileInputStream().close();
			}
        } catch (IOException e) {
            e.printStackTrace();
        }



And i only get the Exception for binary Files, like Images.

i tried also this way, with same results.
Dont work for binary files, all other file are fine.

        try {
            if (uploadItem.getFileInputStream() != null) {
            	
				uploadItem.getFileInputStream().close();
			}
        } catch (IOException e) {
            e.printStackTrace();
        }
        
       InputStream fin;
       int ch;
       StringBuffer strContent = new StringBuffer("");
		try		{
			fin = uploadItem.getFileInputStream();
			while ((ch = fin.read()) != -1)	strContent.append((char) ch);
			fin.close();

		}

		catch (Exception e)

		{
			e.printStackTrace();
		}



I also tried it with a buffered read , same results.
Works with e.g. text files , dont works with binary files

        try {
            if (uploadItem.getFileInputStream() != null) {
            	
				uploadItem.getFileInputStream().close();
			}
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        InputStream is = uploadItem.getFileInputStream();
        
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        
        StringBuilder sb = new StringBuilder();
        String line = null;
        
		try		{

	        while ((line = reader.readLine()) != null) {
	          sb.append(line + "\n");
	        }
	        is.close();
	        System.out.println(sb.toString());
		}
		catch (Exception e)

		{
			e.printStackTrace();
		}

[Updated on: Fri, 04 December 2009 13:15]

Report message to a moderator

Re: Write Image uploaded by Upload Widget to File [message #502078 is a reply to message #501910] Sat, 05 December 2009 09:29 Go to previous messageGo to next message
Ivan Furnadjiev is currently offline Ivan FurnadjievFriend
Messages: 2359
Registered: July 2009
Location: Sofia, Bulgaria
Senior Member
Hannes,

if you want to read from the input stream don't close it.

Best,
Ivan
Re: Write Image uploaded by Upload Widget to File [message #504162 is a reply to message #501507] Thu, 17 December 2009 14:04 Go to previous message
Aleksandar Pavlov is currently offline Aleksandar PavlovFriend
Messages: 79
Registered: July 2009
Member
Hi,
Once upon a time I found out that the upload widget returns more bytes,
than there are actually in the file to be loaded.
bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=289708

This is a wild guess, but might be helpful.
Aleks

hannes wrote:
> Hello,
>
> im trying to write the FileInputStream of an uploaded jpeg to a file.
>
> Im using the following code
> if(uploadItem.getContentType().equals("image/jpeg"));{
>
> ImageRegistry registry = new
> ImageRegistry(Display.getCurrent());
> registry.put("image", ImageDescriptor.createFromImage(new
> Image(Display.getCurrent(), uploadItem.getFileInputStream())));
> Image image = registry.get("image");
> byte[] imageData =
> image.getImageData().data;
> try {
> fos = new
> FileOutputStream(properties.getProperty("imagePath")+uploadItem.getFileName());
>
> fos.write(imageData);
> fos.close();
> } catch (Exception e1) {
> // TODO Auto-generated catch block
> e1.printStackTrace();
> }
>
> }
>
>
>
> But this throws the following Exception
>
> ## total: 2395521
> filename : IMGP0847.JPG
> path : IMGP0847.JPG
> content-type : image/jpeg
> stream : java.io.FileInputStream@cb754f
> 2009-12-02 16:28:07.796::WARN: ERROR: /rap
> org.eclipse.rwt.internal.resources.ResourceRegistrationExcep tion: Failed
> to register resource 'image-16491778'.
> at
> org.eclipse.rwt.internal.resources.ResourceManagerImpl.regis ter(ResourceManagerImpl.java:239)
>
> at
> org.eclipse.rap.ui.internal.servlet.ResourceManagerFactory$R esourceManagerWrapper.register(ResourceManagerFactory.java:1 30)
>
> at
> org.eclipse.swt.internal.graphics.ResourceFactory.registerIm age(ResourceFactory.java:346)
>
> at org.eclipse.swt.graphics.Image.init(Image.java:178)
> at org.eclipse.swt.graphics.Image.<init>(Image.java:121)
>
>
> Can anyone help me please ?
>
> I also tried to write out the InputStream byte by byte, but this also
> dont work.
>
> Regards,
> hannes
Previous Topic:NoClassDefFoundError with RAP 1.3
Next Topic:Custom RichText Widget in an Editor, problem with markDirty()
Goto Forum:
  


Current Time: Sun Dec 05 05:22:03 GMT 2021

Powered by FUDForum. Page generated in 0.02996 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top