掲示板

CSV File Upload

11年前 に Rahul Mahajan によって更新されました。

CSV File Upload

New Member 投稿: 10 参加年月日: 12/07/03 最新の投稿
Hi,

i want to upload records from CSV file to one of a database table. please suggest the steps or any link which shares the same.

Thanks
Rahul Mahajan
thumbnail
11年前 に Jignesh Vachhani によって更新されました。

RE: CSV File Upload

Liferay Master 投稿: 803 参加年月日: 08/03/10 最新の投稿
You can create simple portlet which would have file upload functions which will read the CSV file.
Now there are so many API available which can parse CSV file and can read data.
So you can use Liferay service builder concept which will create database table schema as per your requirment and so now you can
read data one by one from CSV file and can make entry in DB by calling service generated by Liferay service builder.
Hope this information is enough to get start.
11年前 に Rahul Mahajan によって更新されました。

RE: CSV File Upload

New Member 投稿: 10 参加年月日: 12/07/03 最新の投稿
Thank you so much Jignesh!
11年前 に Rahul Mahajan によって更新されました。

RE: CSV File Upload

New Member 投稿: 10 参加年月日: 12/07/03 最新の投稿
i am using FileEntry(existing liferay class) to upload the file and trying to read the file by CSV reader ( Utility to read csv file)

but stuck at - below line- i want to pass the file path here but file entry object doesnt provides any methods which gives file path.
CSVReader reader = new CSVReader(new FileReader(//dont know how i can get the path of the file on server ));

code--

UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
String sourceFileName = uploadRequest.getFileName("fileUpload");//uploaded filename
String title = uploadRequest.getParameter("title");
String description = uploadRequest.getParameter("description");
long repositoryId = ParamUtil.getLong(uploadRequest, "repositoryId");
long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID; //get home directory folderId
String changeLog = "changeLog";
InputStream inputStream = null;
int fileSize =(int) uploadRequest.getSize("fileUpload");
String contentType = uploadRequest.getContentType("fileUpload");
inputStream = uploadRequest.getFileAsStream("fileUpload");
ServiceContext serviceContext;
serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(), actionRequest);
FileEntry fileEntry = DLAppServiceUtil.addFileEntry(repositoryId, folderId, sourceFileName, contentType, title,description, changeLog, inputStream, fileSize, serviceContext);

// csv reader code
CSVReader reader = new CSVReader(new FileReader(//sourceFileName- what to pass here ));
ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
strat.setType(ProviderInfoModelImpl.class);
String[] columns = new String[] {"name", "orderNumber", "id"}; // the fields to bind do in your JavaBean
strat.setColumnMapping(columns);
CsvToBean csv = new CsvToBean();
List list = csv.parse(strat,reader );

Please suggest
thumbnail
11年前 に Archi Madhu によって更新されました。

RE: CSV File Upload

Regular Member 投稿: 237 参加年月日: 08/03/25 最新の投稿
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest
.getAttribute(WebKeys.THEME_DISPLAY);

UploadPortletRequest uploadRequest = PortalUtil
.getUploadPortletRequest(actionRequest);

File fileupload = uploadRequest.getFile("file");
int pos = filename.lastIndexOf('.');
String fileExtension = filename.substring(pos + 1);
if (Validator.isNull(fileExtension)
|| !(fileExtension.equalsIgnoreCase("csv"))) {
throw new NoSuchFileException();
}
if (fileExtension.equalsIgnoreCase("csv") /*|| (fileExtension == "csv")*/) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(
fileupload.getAbsolutePath()));

for (int itri = 0; ; itri++) {
String strLine = br.readLine();
if( strLine == null){
break;
}

log.info("Line number " + itri + " value : " + strLine);
}

// YOUR CODE GOES HERE
}
}

HTH!
-Archi
thumbnail
11年前 に Amit Doshi によって更新されました。

RE: CSV File Upload

Liferay Master 投稿: 550 参加年月日: 10/12/29 最新の投稿
Rahul Mahajan:
i am using FileEntry(existing liferay class) to upload the file and trying to read the file by CSV reader ( Utility to read csv file)

but stuck at - below line- i want to pass the file path here but file entry object doesnt provides any methods which gives file path.
CSVReader reader = new CSVReader(new FileReader(//dont know how i can get the path of the file on server ));

code--

UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
String sourceFileName = uploadRequest.getFileName("fileUpload");//uploaded filename
String title = uploadRequest.getParameter("title");
String description = uploadRequest.getParameter("description");
long repositoryId = ParamUtil.getLong(uploadRequest, "repositoryId");
long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID; //get home directory folderId
String changeLog = "changeLog";
InputStream inputStream = null;
int fileSize =(int) uploadRequest.getSize("fileUpload");
String contentType = uploadRequest.getContentType("fileUpload");
inputStream = uploadRequest.getFileAsStream("fileUpload");
ServiceContext serviceContext;
serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(), actionRequest);
FileEntry fileEntry = DLAppServiceUtil.addFileEntry(repositoryId, folderId, sourceFileName, contentType, title,description, changeLog, inputStream, fileSize, serviceContext);

// csv reader code
CSVReader reader = new CSVReader(new FileReader(//sourceFileName- what to pass here ));
ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
strat.setType(ProviderInfoModelImpl.class);
String[] columns = new String[] {"name", "orderNumber", "id"}; // the fields to bind do in your JavaBean
strat.setColumnMapping(columns);
CsvToBean csv = new CsvToBean();
List list = csv.parse(strat,reader );

Please suggest


I was using CSV Reader in one of my project with exactly the same requirement as you have.

For that CSV file path, please check below code :-


private CsvReader getCSV(UploadPortletRequest portletRequest) {
		
		InputStream inputStream = null;
		CsvReader csv=null;
		try {
			inputStream = new FileInputStream(portletRequest.getFile("fileupload"));
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			LOG.error("Error on Reading File"+e.getMessage());
		}
		BufferedReader csvFile = 
	        new BufferedReader(new InputStreamReader(inputStream));
		csv = new CsvReader(csvFile);
		
		return csv;
	}



Let me know if any issues.

Thanks & Regards,
Amit Doshi
11年前 に Rahul Mahajan によって更新されました。

RE: CSV File Upload

New Member 投稿: 10 参加年月日: 12/07/03 最新の投稿
Still no luck

javax.portlet.PortletException: java.io.FileNotFoundException: D:\liferay-portal-tomcat-6.1.0-ce-ga1\tomcat-7.0.23\temp\upload_00000014.csv

also i varified there is no file with given name in tem folder.
thumbnail
11年前 に Amit Doshi によって更新されました。

RE: CSV File Upload

Liferay Master 投稿: 550 参加年月日: 10/12/29 最新の投稿
Rahul Mahajan:
Still no luck

javax.portlet.PortletException: java.io.FileNotFoundException: D:\liferay-portal-tomcat-6.1.0-ce-ga1\tomcat-7.0.23\temp\upload_00000014.csv

also i varified there is no file with given name in tem folder.



Why the temp folder come into picture I can't understand that...

Do Simple things ..

1) clear the temp and work folder.
2) Restart the Server.
3) Use below code for jsp.


<aui:form action="<%=actionURL%>" enctype="multipart/form-data" method="post" name="<%=Constants.IMPORT_ACTION %>">
	</aui:form>
<table> <tbody><tr> <td class="lbl"><liferay-ui:message key="select-file-to-upload" /></td> <td><input type="file" name="fileupload" id="fileupload" size="40"></td> </tr> </tbody></table> <aui:button type="submit" value="upload" name="upload" cssClass="topSpace" />


4) Java Code.

UploadPortletRequest upreq = PortalUtil.getUploadPortletRequest(portletRequest); // you can directly cast from renderRequest to portletRequest.
CsvReader csv =getCSV(upreq);


5) Use same function that I have posted in my post above.

6) Your work will be done.

Thanks & Regards,
Amit Doshi
11年前 に Rahul Mahajan によって更新されました。

RE: CSV File Upload

New Member 投稿: 10 参加年月日: 12/07/03 最新の投稿
Thanks a Ton Amit. this is wroking file now
thumbnail
11年前 に Amit Doshi によって更新されました。

RE: CSV File Upload

Liferay Master 投稿: 550 参加年月日: 10/12/29 最新の投稿
Good to know that.. emoticon At last it fixed your issue.
thumbnail
10年前 に technology evangelist によって更新されました。

RE: CSV File Upload

Junior Member 投稿: 35 参加年月日: 13/08/02 最新の投稿
Amit Doshi:
Good to know that.. emoticon At last it fixed your issue.


Hi Amit Doshi,

My task is read data from excel file(Category and tag) and according to that it need to store into document library
.Through Excel File will not read by java io .
I hope These is correct.If its wrong correct me

.So I convert excel type file into csv file format. So i am passing csv file path as String (String path="//doc//dfg.csv").In csv File reader , I am passing these int CVS reader as like
CsvReader csvReader =new CsvReader(new FileReader(path));
But Initially its throw error ,So I had include opencsv1.8 jar file in my build properties.After that error is gone in my workspace.But during my program execution its showing console error fro above code .So help me to resolve these issuse .

Following Below things I need to clarify answer for it.

* Its recommended using csv file format or otherwise need to use apache poi for reading excel file
* If above code is correct then,then there will be error due to Jar File for open csv


Best Wishes
thumbnail
10年前 に Amit Doshi によって更新されました。

RE: CSV File Upload

Liferay Master 投稿: 550 参加年月日: 10/12/29 最新の投稿
Hi Technology,

First thing, I don't understand why you are converting Excel file into CSV file?

Instead you directly read the Excel file.

I am using POI api for Exporting the Excel File at couple of places and it is working fine for me.

So, Use directly the POI, instead of doing conversion from CSV to Excel.

Let me know if required more details.

Thanks & Regards,
Amit Doshi
thumbnail
10年前 に technology evangelist によって更新されました。

RE: CSV File Upload

Junior Member 投稿: 35 参加年月日: 13/08/02 最新の投稿
Amit Doshi:
Hi Technology,

First thing, I don't understand why you are converting Excel file into CSV file?

Instead you directly read the Excel file.

I am using POI api for Exporting the Excel File at couple of places and it is working fine for me.

So, Use directly the POI, instead of doing conversion from CSV to Excel.

Let me know if required more details.

Thanks & Regards,
Amit Doshi



Hi Amit Doshi],

Thanks for your reply.I am resolved my issue by including jar file opencsv2.0 in both my properties and also lib folder in tomcat .Amit Paste apache poi code .
that will help somebody like me
Best Wishes,
5年前 に gopal vaghasiya によって更新されました。

RE: CSV File Upload

New Member 投稿: 5 参加年月日: 18/01/18 最新の投稿
can you give me an example or snippet for the same

Thanks,
Gopal