i am trying to upload a file with either *.txt extension or *.abcx extension to the server from a client location
i am using richfaces fileupload for the same purpose.
this a java servlet code...which i tried and it works perfectly the way i want
but i have to use jsf and bean classes
<%@ page language="java" %> <HTml> <HEAD> <TITLE>Display file upload form to the user</TITLE> </HEAD> <% // for uploading the file we used Encrypt type of multipart/form-data and input of file type to browse and submit the file %> <BODY> <FORM ENCTYPE="multipart/form-data" ACTION="singleupload.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> <%@ page import="java.io.*" %> <% //to get the content type information from JSP Request Header String contentType = request.getContentType(); out.print(contentType); //here we are checking the content type is not equal to Null and as well as the passed data from mulitpart/form-data is greater than or equal to 0 if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) { DataInputStream in = new DataInputStream(request.getInputStream()); out.print("input stream"+request.getInputStream()); //we are taking the length of Content type data int formDataLength = request.getContentLength(); out.print("request content length"+request.getContentLength()); byte dataBytes[] = new byte[formDataLength]; out.print("request content length"+formDataLength); 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; // creating a new file with the same name and writing the content in new file String filePath="D:\\download\\"+saveFile; out.print(filePath); FileOutputStream fileOut = new FileOutputStream(filePath); 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> <% }%>
the jsp page code <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%> <%@ taglib prefix="rich" uri="http://richfaces.org/rich" %> <%@ taglib prefix="a4j" uri="http://richfaces.org/a4j" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <f:view> <h:form> <rich:fileUpload fileUploadListener="#{load.listener}" addControlLabel="Browse" maxFilesQuantity="1" id="upload1" listHeight="50" listWidth="300" acceptedTypes="abdx,txt" > </rich:fileUpload> </h:panelGrid> <h:commandButton value="Open Project" action="#{Upload.showPanel}" style="width: 92px; height: 40px" onclick="Enable()"> </h:commandButton> <rich:modalPanel showWhenRendered="#{Uload.panelVisible}" id="panel4" width="550" height="180" > <f:facet name="header"> <h:panelGroup> <h:outputText value="Select the location to save the file"></h:outputText> </h:panelGroup> </f:facet> <f:facet name="controls"> <h:panelGroup> <h:graphicImage value="images/close1.jpg" id="hidemsg"/> <rich:componentControl for="panel4" attachTo="hidemsg" operation="hide" event="onclick"> <!- <a4j:support event="oncomplete" reRender="assignment"></a4j:support> --> </rich:componentControl> </h:panelGroup> </f:facet> <a4j:form> <rich:fileUpload fileUploadListener="#{Upload.listenerRead}" addControlLabel="Browse" maxFilesQuantity="1" id="upload2" listHeight="100" listWidth="500" acceptedTypes="abdx,txt" > </rich:fileUpload> </a4j:form> </rich:modalPanel> </h:form> <f:view> </body> </html> bean class method will be of class load : public void listener(UploadEvent event) throws Exception{ UploadItem item = event.getUploadItem(); System.out.println("$##### "+item.getFileName()); filePath=new File(item.getFileName()); //File file = item.getFile(); /*this.filePath = file;*/ this.choosenFile = filePath.getAbsolutePath(); // Approve (Open or Save) was clicked String filename=filePath.getName(); System.out.println(filename); } public String showPanel(){ panelVisible=true; return "success"; } public void listenerRead(UploadEvent event) throws Exception{ UploadItem item = event.getUploadItem(); System.out.println("** "+item.getFileName()); filePath=new File(item.getFileName()); }
what else could be added to the bean class to do the same as the previous above one.
the above code with modal pannel, i am not able to invoke.
i am not able to get the request.getInputStream() from the uploaded file
please provide me with a solution so that i can solve the above problem from the fileupload method of richfaces, with the help of
uploadevent