File Upload / Download
Here is explanation of file upload and file download using FileUpload and FileDownload component.
File Upload/ Download preparation
If we want to use FileUplaod, FileDownload component we should add on TypeDefinition. After clicking Add in TypeDefinition window, we can input add ID and ClassName directly.
Adding files
After clicking on 'select' on FileComponent we can select file. In case of we want to add multiple files we should click on 'Add file' button, a new Fileupload component will be appeared. Following this process we can add multiple files. Added files will be appeared on the Grid.
File upload
After uploading file on the server we vertify whether the file is uploaded on server successfully.
After file upload you can verify whether file is uploaded to location server file path normally.
When you upload files via Nexacro Platform wherein the file size can not be recognized prior to uploading to the web server.
In this case, you have to upload files to server then you should check the file size using JSP.
Downloading files
If we want to download the files, we can download files on local selecting by checkbodx.
File deletion
Using Fileupload component we can select the files by checkbox. We can select multiple rows from the Fileupload component for deletion. After clicking on the "File delete" button the files will be deleted from File component. For example we want to delete second image 'Combo_Basic_1.png', so select the file checkbox from the Filieupload component. After clicking on the Delete file button, the file will be deleted from File component.
Main source content
JSP Source for Upload/Download
fileupload.jsp
<%@ page contentType="text/html;charset=euc-kr" %> <%@ page language="java"%> <%@ page import="java.io.File"%> <%@ page import="java.io.IOException"%> <%@ page import="java.util.ArrayList"%> <%@ page import="java.util.Enumeration"%> <%@ page import="java.util.Iterator"%> <%@ page import="java.util.List"%> <%@ page import="javax.servlet.ServletException"%> <%@ page import="javax.servlet.http.HttpServletRequest"%> <%@ page import="javax.servlet.http.HttpServletResponse"%> <%@ page import="com.oreilly.servlet.MultipartRequest"%> <%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%> <%@ page import="com.tobesoft.xplatform.data.ColumnHeader"%> <%@ page import="com.tobesoft.xplatform.data.DataSet"%> <%@ page import="com.tobesoft.xplatform.data.DataTypes"%> <%@ page import="com.tobesoft.xplatform.data.PlatformData"%> <%@ page import="com.tobesoft.xplatform.data.VariableList"%> <%@ page import="com.tobesoft.xplatform.tx.HttpPlatformResponse"%> <%@ page import="com.tobesoft.xplatform.tx.PlatformException"%> <% String chkType = request.getHeader("Content-Type"); //System.out.println(chkType); if( chkType == null ) return; request.setCharacterEncoding("utf-8"); String contextRealPath = request.getSession().getServletContext().getRealPath("/"); String PATH = request.getParameter("PATH"); String savePath = contextRealPath + PATH; System.out.println(savePath); System.out.println(savePath); int maxSize = 500 * 1024 * 1024; // Maximum limit for file upload size 500MB PlatformData resData = new PlatformData(); VariableList resVarList = resData.getVariableList(); String sMsg = " A "; try { MultipartRequest multi = new MultipartRequest(request, savePath, maxSize, "utf-8", new DefaultFileRenamePolicy()); Enumeration files = multi.getFileNames(); // Getting all file names sMsg += "B "; DataSet ds = new DataSet("Dataset00"); ds.addColumn(new ColumnHeader("fileName", DataTypes.STRING)); ds.addColumn(new ColumnHeader("fileSize", DataTypes.STRING)); ds.addColumn(new ColumnHeader("fileType", DataTypes.STRING)); sMsg += "C "; String fileName=""; while (files.hasMoreElements()) { sMsg += "D "; String name = (String)files.nextElement(); fileName += multi.getFilesystemName(name); String type = multi.getContentType(name); File f = multi.getFile(name); System.out.println("fileName:"+fileName); System.out.println("fileSize:"+f.length()); int row = ds.newRow(); ds.set(row, "fileName", fileName); ds.set(row, "fileType", type); if (f != null) { String size = Long.toString(f.length()/1024)+"KB"; ds.set(row, "fileSize", size); } sMsg += row +" "; } resData.addDataSet(ds); resVarList.add("ErrorCode", 200); //resVarList.add("ErrorMsg", savePath+"/"+fileName); resVarList.add("ErrorMsg", fileName); } catch (Exception e) { resVarList.add("ErrorCode", 500); resVarList.add("ErrorMsg", sMsg + " " + e); } HttpPlatformResponse res = new HttpPlatformResponse(response); res.setData(resData); res.sendData(); %>
fileDownload.jsp
<%@ page contentType="text/html;charset=euc-kr" %> <%@ page language="java"%> <%@ page import="java.io.BufferedInputStream"%> <%@ page import="java.io.FileInputStream"%> <%@ page import="java.io.File"%> <%@ page import="java.io.IOException"%> <%@ page import="javax.servlet.ServletException"%> <%@ page import="javax.servlet.ServletOutputStream"%> <%@ page import="javax.servlet.http.HttpServletRequest"%> <%@ page import="javax.servlet.http.HttpServletResponse"%> <%@ page import="com.oreilly.servlet.MultipartRequest"%> <%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%> <% String contextRealPath = request.getSession().getServletContext().getRealPath("/"); String savePath = contextRealPath + "file"; String name = request.getParameter("file"); String filename = new String(name.getBytes("iso8859-1"), "UTF-8"); byte[] buffer = new byte[1024]; ServletOutputStream out_stream = null; BufferedInputStream in_stream = null; File fis = new File(savePath + "/" + filename); if(fis.exists()){ try{ response.setContentType("utf-8"); response.setContentType("application/octet;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + filename); out.clear(); out = pageContext.pushBody(); out_stream = response.getOutputStream(); in_stream = new BufferedInputStream(new FileInputStream(fis)); int n = 0; while ((n = in_stream.read(buffer, 0, 1024)) != -1) { out_stream.write(buffer, 0, n); }// while } catch (Exception e) { e.printStackTrace(); } finally { if (in_stream != null) { try { in_stream.close(); } catch (Exception e) {} } if (out_stream != null) { try { out_stream.close(); } catch (Exception e) {} } } }else{ response.sendRedirect("unknownfile"); } %>
Source Location
Sample\FileUpDownload\np_FileUpDownload.xfdl