Hi,

My requirement is like this:
I need to select a csv or excel file, from the local machine,read a column
from it and store all the strings of a particular column in a List of a
object and store it in a database.
I am using Flex to upload the file and trying to read the file from
struts2,but in my action class i am unable to get the uploaded file.My code
is as follows,it would be very very useful if somebody tries to help me out
of this.Please let me know If I have gone wrong anywhere.


1)mxml file

?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; layout="absolute">
<mx:Script>
<![CDATA[
import ImportData;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.events.IOErrorEvent;


[Bindable] var fileRef:FileReference = new FileReference();
private function openFileDialog():void{
fileRef.addEventListener(Event.SELECT, selectHandler);
fileRef.addEventListener(Event.COMPLETE, completeHandler);
fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA
,uploadCompleteHandler);
fileRef.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
try{
var textTypes:FileFilter = new FileFilter("Text Files
(*.txt,*.csv)","*.txt;*.csv");
var allTypes:Array = new Array(textTypes);
//var success:Boolean = fileRef.browse();
var success:Boolean = fileRef.browse(allTypes);
}
catch(error:Error){
trace("Unable to browse for files.");
}
}

private function onIOError(event:IOErrorEvent):void {
trace("In here"+event.text);
trace("In here"+event.toString());
}
// when a file is selected you upload the file to the upload script on the
server
private function selectHandler(event:Event):void{
//var request:URLRequest = new URLRequest("/importAction");
var request:URLRequest = new URLRequest(" 
try
{
fileRef.upload(request);
}
catch (error:Error)
{
trace("Unable to upload file.");
}
}

private function completeHandler(event:Event):void{
trace("uploaded");
}

// dispatched when file has been uploaded to the server script and a
response is returned from the server
// event.data contains the response returned by your server script

public function uploadCompleteHandler(event:DataEvent):void {
trace("uploaded... response from server: \n" + String(event.data));
}
]]>
</mx:Script>
<mx:Button label="Import" id="importBtn" click="openFileDialog()"
height="20" width="90" styleName="buttonsOnSearchBar"/>
<mx:ComboBox x="23" y="44" borderColor="#ff0000"
themeColor="#ff0000"></mx:ComboBox>

</mx:Application>

2)struts.xml file 

<struts>
<package name="pack1" extends="struts-default,json-default"> 
<global-results>
<result name="error" type="json"></result>
</global-results> 

<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Throwable"/>
</global-exception-mappings>

<action name="importAction" class="routing.ImportAction">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/> 
<result name="success" type="json"></result>
</action>
</package>
</struts>

3)Action Class


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;

public class ImportAction extends ActionSupport{

private String contentType;
private File upload;
private String fileName;
private String caption;

private static final Logger logger = Logger.getLogger(ImportAction.class);



@Override
public String execute() throws Exception {

/**
* Read File Line by Line.. If the file has more than one word separated by
comma
* return error.
* 
* 
*/
ArrayList<String> symbolList = new ArrayList<String>();
try{
BufferedReader reader = new BufferedReader(new FileReader(upload));
String line =null;
String symbol=null;
while((line=reader.readLine())!=null){
StringTokenizer tokenizer = new StringTokenizer(line,"\t");
symbol = tokenizer.nextToken();
if(symbol!=null) symbol = symbol.trim();
if(symbol.length()>0)
symbolList.add(symbol);
}
}catch(FileNotFoundException fne){
if(logger.isDebugEnabled())
logger.debug("File NotFount ", fne);
}

for(String symbol1:symbolList)
System.out.print(symbol1+" ");
return SUCCESS;

}
public String getUploadFileName() {
return fileName;
}
public void setUploadFileName(String fileName) {
this.fileName = fileName;
}



public String getUploadContentType() {
return contentType;
}
public void setUploadContentType(String contentType) {
this.contentType = contentType;
}

public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}

public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}

public String input() throws Exception {
return SUCCESS;
}

public String upload() throws Exception {
return SUCCESS;
}
}


-- 
View this message in context: 
http://www.nabble.com/Reading-uploaded-file-tp14501548p14501548.html
Sent from the FlexCoders mailing list archive at Nabble.com.

Reply via email to