Question about the upload component

Hello!
I have probably a silly question according to the upload component of vaadin. I used the code from the sample for my upload component.

I want to do the following:

  • The file should be only uploaded to the server temporarily, because I directly want to read the file and parse the content into a String. For that I already wrote a class which takes the filename as constructor and then proceeds. Now I need your help how I can give the right filename to the constructor.

In usual webapplications the file will be stored in a temporarily folder on the webserver and needs to be copied explicitly to the final destination by the webapplicationcode. I believe in Vaadin this will be similiar.

What do I have to change in the code that I can reach my goal?


public class OpenCSVFile implements Receiver {

	private String fileName;
    private String mtype;
    private int counter;
    
    FileOutputStream fileOutput = null;
    private String actualFilename;

    /**
     * return an OutputStream that simply counts lineends
     */
    public OutputStream receiveUpload(String filename, String MIMEType) {
    	
    	counter = 0;
        fileName = filename;
        mtype = MIMEType;
        
        //File file = new File("/tmp/uploads/" + filename);
        //ReadCsvFile csvFile = new ReadCsvFile(file);
        //csvFile.readFile();

        
        return new OutputStream() {
            private static final int searchedByte = '\n';

            @Override
            public void write(int b) throws IOException {
            	
            	
            	            	
            	System.out.println(":"+b);
                if (b == searchedByte) {
                    counter++;
                }
            }
        };
    }

    public String getFileName() {
        return fileName;
    }

    public String getMimeType() {
        return mtype;
    }

    public int getLineBreakCount() {
    	//File file = new File(fileName);
        //ReadCsvFile csvFile = new ReadCsvFile(file);
        //csvFile.readFile();
        System.out.println("Hello, fileName: "+getFileName());
    	
        return counter;
    }

Thank yout for any help!!!

If you just want to read the file, and not store it, could you just skip the part of storing the file and just directly convert the outputstream into a string?


http://stackoverflow.com/questions/216894/get-an-outputstream-into-a-string

Google for more conversion options.

Thank you for your answer. After googeling I found a code which seams to work for me:

public class UploadBuffer implements StreamSource, Upload.Receiver {
    ByteArrayOutputStream outputBuffer = null;

    String mimeType;

    String fileName;

    public UploadBuffer() {

    }

    public InputStream getStream() {
        if (outputBuffer == null) {
            return null;
        }
        return new ByteArrayInputStream(outputBuffer.toByteArray());
    }

    /**
     * @see com.vaadin.ui.Upload.Receiver#receiveUpload(String,
     *      String)
     */
    public OutputStream receiveUpload(String filename, String MIMEType) {
        fileName = filename;
        mimeType = MIMEType;
        outputBuffer = new ByteArrayOutputStream();
        return outputBuffer;
    }

    /**
     * Returns the fileName.
     * 
     * @return String
     */
    public String getFileName() {
        return fileName;
    }

    /**
     * Returns the mimeType.
     * 
     * @return String
     */
    public String getMimeType() {
        return mimeType;
    }

}

This code works perfectly for me but I have another question. In the gui I have to following lines of code:

private void createHeader() {
		
		upload.addListener(new Upload.FinishedListener() {
            public void uploadFinished(FinishedEvent event) {
            	StreamResource resource = new StreamResource(uploadBuffer, uploadBuffer.getFileName(),main.getApplication());
            	Link userlist = new Link("Download ",resource);
            	main.addComponent(userlist);
            	System.out.println("List: "+userlist.getApplication());
            }
        });
		main.addComponent(upload);
		//uploadLayout.setComponentAlignment(upload, Alignment.MIDDLE_CENTER);
	}

With that code I now have a link which opens the document for me, but as I said before my parser needs the filename. Until now I didnĀ“t find a method from the link which simply returns the ULR to the path. I thought I just take the path and give it to my parser.

Is this a working solution, and if yes how can I simply returns the path of the file?

Thank you for your help and time!

Ok, now the following code is working, it takes the file and convert it into a string. Then you can do with the content whatever you want:

@SuppressWarnings("serial")
public class UploadBuffer implements Receiver {
	
	private String fileName;
    private String mtype;
    private int counter;
    private StringBuilder string = new StringBuilder();
    
	@Override
	public OutputStream receiveUpload(String filename, String MIMEType) {
		
		 fileName = filename;
         mtype = MIMEType;
         return new OutputStream()
         {
        	 
			@Override
             public void write(int b) throws IOException {
                 string.append((char) b );
             }
             
             //Netbeans IDE automatically overrides this toString()
             public String toString(){
                 return string.toString();
             }
         };
	}
	
	 public String getFileName() {
         return fileName;
     }

     public String getMimeType() {
         return mtype;
     }

     public int getLineBreakCount() {
         return counter;
     }
    public String getContent(){
    	return string.toString();
    }
}

Thanks for your help!