Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Uploading .csv or .xlsx files in server through JSP page

Hi all,
I would like to feed data files (in .csv or .xlsx) to a Talend job. The files will be uploaded using JSP. How can I create a link between JSP and Talend files input. The upload can be either a single file or multiple files.
How can I do so?
Any suggestions?
I am quite new in Talend world, specially when it comes to JSP/Talend interaction 🙂
Thanks in advance....
Here is the code for single upload:
HTML:
<%@ page language="java" %>
<HTml>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>
<BODY> <FORM ENCTYPE="multipart/form-data" ACTION="single_upload_page.jsp" METHOD=POST>
<br><br><br>
<center><table border="2" >
<tr><center><td colspan="2"><p align="center"><B>PROGRAM FOR UPLOADING THE FILE</B><center></td></tr>
<tr><td><b>Choose the file To Upload:</b>
</td>
<td><INPUT NAME="F1" TYPE="file"></td></tr>
<tr><td colspan="2">
<p align="right"><INPUT TYPE="submit" VALUE="Send File" ></p></td></tr>
<table>
</center>
</FORM>
</BODY>
</HTML>

JSP
<%@ page import="java.io.*" %>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.io.File"%>
<%
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
//we are taking the length of Content type data
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte;
int byteRead = 0;
int totalBytesRead = 0;
//this loop converting the uploaded file into byte code
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead,
formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
//for saving the file name
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,
contentType.length());
int pos;
//extracting the index of file
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation))
.getBytes()).length;
FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br><table border="2"><tr><td><b>You have successfully
upload the file by the name of:</b>
<% out.println(saveFile); %>
</td></tr></table>
<%
}
%>
Labels (3)
2 Replies
Anonymous
Not applicable
Author

Hi
In the job, use a tFileInputDelimited to read the csv file or tFileInputExcel to read the xlsx file.
Have a look at the topic as below to know how to call a Talend Job in JSP.
https://community.talend.com/t5/Design-and-Development/JSP-java-server-page/td-p/101570
Best regards
Shong
Anonymous
Not applicable
Author

If the problem is to upload file with Talend, try to use tFileFetch component ( POST + upload file ).
Or if you want to process the file in the jsp, you can export talend job as a jar file and then include in the classpath of the runnign jsp container... just as additional code... there are many thread in this forum regarding exporting/calling in java.