Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: [jsp/jsf] Send string(file name) to other class

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [jsp/jsf] Send string(file name) to other class

    Hi i have a little problem
    I have a two class Upload - class upload file to server and class filter
    but now in class filter i need name uploaded file i try something like this:

    class upload:

    public class uploadclass {
     
    	private UploadedFile uploadedFile;
            private String fileName;
     
        public String submit() {
    	String prefix = FilenameUtils.getBaseName(uploadedFile.getName());
            String suffix = FilenameUtils.getExtension(uploadedFile.getName());
     
     
            File file = null;
            OutputStream output = null;
     
            try {
                file = File.createTempFile(prefix , "." + suffix, new File("c:/upload"));
     
                output = new FileOutputStream(file);
                IOUtils.copy(uploadedFile.getInputStream(), output);
     
                [B]fileName = file.getName();
                filter fil=new filter();
               fil.setNazwa(fileName);[/B]
     
                System.out.println(fileName);
     
            } catch (IOException e) {
               if (file != null) file.delete();
               FacesContext.getCurrentInstance().addMessage("uploadForm", new FacesMessage(
               FacesMessage.SEVERITY_ERROR, "File upload failed with I/O error.", null));
               e.printStackTrace();
            }
            finally {
                IOUtils.closeQuietly(output);
             }
            return "success";
        }
       public UploadedFile getUploadedFile() {
            return uploadedFile;
        }
        public  String getFileName() {
        	return fileName;
        }
        public void setUploadedFile(UploadedFile uploadedFile) {
            this.uploadedFile = uploadedFile;
        }
     
    }
    this:
    fileName = file.getName();
    filter fil=new filter();
    fil.setNazwa(fileName);


    And filter class
    public class filter {
    	private String name;
    	public String filename;
     
    	public void aaa(){
    	        filename=getName();
    		System.out.println(filename);
    	}
    	public void setName(String name) {
    	      this.name = name;
    	 }
    	 public String getName() {
    	      return name;
    	 }
     
    }


    When System.out.println(filename); is execute i have filename on console null;
    submit() and aaa() is active on buttonclick
    what i doing wrong?
    Last edited by barni120; February 7th, 2012 at 08:09 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: [jsp/jsf] Send string(file name) to other class

    i have filename on console null;
    Where in the code is filename given a non-null value? The only way you will know if the value is not null is if you print it.
    Add printlns to all the places where a value is passed and set to see where the values are coming from and how they are being set.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: [jsp/jsf] Send string(file name) to other class


  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [jsp/jsf] Send string(file name) to other class

    Quote Originally Posted by Norm View Post
    Where in the code is filename given a non-null value? The only way you will know if the value is not null is if you print it.
    Add printlns to all the places where a value is passed and set to see where the values are coming from and how they are being set.
    Quote Originally Posted by Norm View Post
    Where in the code is filename given a non-null value?
    here i creat a new file on disk file = File.createTempFile(prefix , "." + suffix, new File("c:/upload"));
    and then fileName = file.getName();

    examlpe i upload Desert.jpg file
    then in fileName i have something like this Desert179344675636454681.jpg

    in this public void setName(String name) {
    this.name = name;
    System.out.println(nazwa+"setnazwa");
    }
    i have Desert179344675636454681.jpg

    but when i try to get file name
    public String getNazwa() {
    System.out.println(nazwa+"getnazwa");
    return nazwa;
    }

    file name is null...

    this construction is correct filename=getNazwa(); ?


    sry for cross post but i search help everywhere....
    Last edited by barni120; February 8th, 2012 at 06:20 AM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: [jsp/jsf] Send string(file name) to other class

    in this
    public void setName(String name) {
    this.name = name;
    System.out.println(nazwa+"setnazwa");
    }
    i have Desert179344675636454681.jpg
    How do you know the value of name here? You do not print it out.

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [jsp/jsf] Send string(file name) to other class

    sry my bad there is a:
    public void setName(String name) {
    this.name = name;
    System.out.println(name+"setname");
    }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: [jsp/jsf] Send string(file name) to other class

    For debugging You need to start with the first place the name is set and print it out. Then every time its value is changed, print it out. One of those print outs will show it being set to a null value.
    If you don't see that happening, then you need to check if there are more than one instances of the class.
    If there are more than one, is that expected? Do all of them have the name value set? The code could set the value in one instance and then later read the value from another instance that is still null.

    To see if there are more than one instance of the class being created, add a println statement to the constructor to print out a message when an instance of the class is created.

  8. #8
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [jsp/jsf] Send string(file name) to other class

    ok thx for help it was working corectly but i wrong understend code
    but now i have other problem

    I have read image to two-dimensional array
    File file = new File("C:/upload/1.bmp");
        		BufferedImage image = ImageIO.read(file);
        		int x=image.getWidth();  
            	int y=image.getHeight(); 
            	int[][]tab_pix=new int[x][y];
     
            	 for(int i=0; i<x;i++)
                 {
                         for(int j=0; j<y;j++){
                                 tab_pix[i][j]=image.getRGB(i,j);
                         }
                 }

    then use median filter
    and how can i save that array to image on disk?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: [jsp/jsf] Send string(file name) to other class

    Do the opposite of what your code does. Create an image, call the set method in a loop and write it to disk

  10. #10
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [jsp/jsf] Send string(file name) to other class

    thx for help
    but next problem... i can't display uploaded file on side
    i do something like this

    side.jsp
    <h:graphicImage id="up" value="C:/upload/#{uploadclass.ImgBeforeFilter}" > </h:graphicImage>

    and in uploadclass:

    public String ImgBeforeFilter(){
    		String img_before=getFileName();
    		return img_before;
    	}



    and i have exception
    javax.servlet.ServletException: /side.jsp(16,0) 'C:/upload/#{uploadclass.ImgBeforeFilter}' Property 'ImgBeforeFilter' not found on type package1.uploadclass
    javax.faces.webapp.FacesServlet.service(FacesServl et.java:321)
    org.apache.myfaces.webapp.filter.ExtensionsFilter. doFilter(ExtensionsFilter.java:357)


    EDIT

    OK nvm
    I have to

    public void setImg(String img) {
    this.img=img;
    }
    public String getImg() {return img;}
    Last edited by barni120; February 9th, 2012 at 06:15 PM.

  11. #11
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [jsp/jsf] Send string(file name) to other class

    ok display work correct but i run it on server tomcat
    and now when i open this web application on other computer
    http://localhost:8080/jee/faces/index.jsp

    and i upload image
    then we are go to filter.jsp where we should see image before filtering
    but image isn't display because we have this:

    <h:graphicImage id="up" value="C:/upload/#{uploadclass.imgBefore}" > </h:graphicImage>
    C:/upload/#{uploadclass.imgBefore}

    and this try open file from computer on we are connect to web application
    how to change the value to retrieve the image from the server?

  12. #12
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: [jsp/jsf] Send string(file name) to other class

    Place all of the related images, files etc in your application package and give them the relative path instead of absolute.

  13. #13
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [jsp/jsf] Send string(file name) to other class

    a take relative path something like this

    ExternalContext context=FacesContext.getCurrentInstance().getExternalContext();
        String mainpath=context.getRealPath("WEB-INF/upload/");
    i have path like this:
    C:\Users\user\Desktop\eclipse-jee-indigo-SR1-win32-x86_64\.metadata\.plugins\org.eclipse.wst.server.c ore\tmp1\wtpwebapps\jee\WEB-INF\upload
    this is corect?

    on side i try display image
    <h:graphicImage id="up" value="WEB-INF/upload/a.png" > </h:graphicImage>
    and the path on side to image is
    localhost:8080/jee/WEB-INF/upload/a.png

    image is not display
    what is wrong?

    and how can i place all of the related images in your application package?
    Last edited by barni120; February 10th, 2012 at 05:10 PM.

  14. #14
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: [jsp/jsf] Send string(file name) to other class

    Please read this. Absolute and Relative Paths

Similar Threads

  1. Send file via DatagramSocket
    By ToshX in forum Java Networking
    Replies: 1
    Last Post: December 2nd, 2011, 03:13 PM
  2. [SOLVED] Send File (path) as argument to method
    By efluvio in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 31st, 2011, 01:13 PM
  3. Platform Issue XP vs 7 ?? Send File Accross Network
    By dumb_terminal in forum Java Networking
    Replies: 0
    Last Post: April 12th, 2011, 02:44 PM
  4. Send Scanned File to E-mail
    By babywiz21 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 2nd, 2011, 08:40 PM
  5. [SOLVED] Server Client does not send file
    By Kakashi in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 10th, 2011, 12:38 PM