i use glassfish server.. using netbeans for jsp... i wnat to upload a file to a folder 'doc' and insert corresponding data about file into database lib. i use navicat Mysql ...
i use this code...
<%@ page import="java.util.List" %> <%@ page import="java.util.Iterator" %> <%@ page import="java.io.File" %> <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%> <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%> <%@ page import="org.apache.commons.fileupload.*"%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.io.*,java.sql.*,java.util.zip.*" %> <%@ page language="java" import="java.util.*,java.text.*" %> <%! String fileType=""; int cnt=0; int count1=0,count2=0,count3=0,count4=0,count5=0,count6=0; %> <% boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (!isMultipart) { String id=request.getParameter("fileId"); String path1=request.getParameter("filePath"); String fileName=request.getParameter("fileNname"); String ftype=request.getParameter("fileType"); String file=request.getParameter("file"); }else { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List items = null; try { items = upload.parseRequest(request); } catch (FileUploadException e) { e.printStackTrace(); } Iterator itr = items.iterator(); while (itr.hasNext()) { FileItem item = (FileItem) itr.next(); if (item.isFormField()) { String name = item.getFieldName(); String value = item.getString(); if(name.equals("fileType")) { fileType=value; } } else { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:DNSlib"); Statement st=con.createStatement(); String id=session.getAttribute("fileid").toString(); ResultSet rs=st.executeQuery("select count(*) from filePath where fileId='"+id+"' and fileType='PDF'"); while(rs.next()) { cnt=rs.getInt(1); } if(cnt>0){ out.println("You can only uploaded one file with a unique file Id..! please upload curresponding image files start new upload"); %><font size="+3" color="white"> <TABLE> <tr><td><form name="form1" action="closeupload.jsp" method="post" > <button type="submit">new upload</input></form> </TABLE> <TABLE> <TR><TD><form name="form2" action="upload12.jsp" method="post" > <button type="submit">continue</button></form> </TABLE><% }else{ String itemName = item.getName(); File savedFile = new File("C:/Users/nidi/Documents/NetBeansProjects/Library/web/doc/"+itemName); item.write(savedFile); String fname=session.getAttribute("filename").toString(); String path =( "C:/Users/nidi/Documents/NetBeansProjects/Library/web/doc/"+itemName); String fileP = new File(path).getName(); int i=st.executeUpdate ("insert into filePath(fileId,filePath,fileName,fileType) values("+id+",'"+fileP+"','"+fname+"','"+fileType+"')"); session.setAttribute("path1" , savedFile); response.sendRedirect("upload12.jsp"); }} catch (Exception e) { e.printStackTrace(); } } } } %>
my problem...: firstly org.apache.commons.fileupload.servlet.ServletFileUpload not found.... i insert jar file commons-fileupload-1.1.1. then problem solved...
bt real problem is when i upload files fusing mozilla browser...it shows like this...
problem loading page..
The connection was reset The connection to the server was reset while the page was loading.
The site could be temporarily unavailable or too busy. Try again in a few moments. If you are unable to load any pages, check your computer's network connection.
If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web. plz help me...
Here is a jsp application to upload any file and save it to database.
1)page.jsp
<%@ page language="java" %> <Html> <HEAD><TITLE>Display file upload form to the user</TITLE></HEAD> <BODY> <FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST> <br><br><br> <center><table border="2" > <tr><center><td colspan="2"><p align="center"><B>UPLOAD THE FILE</B><center></td></tr> <tr><td><b>Choose the file To Upload:</b> </td> <td><INPUT NAME="file" 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>
2)upload.jsp
<%@ page import="java.io.*" %> <%@ page import="java.sql.*" %> <% String contentType = request.getContentType(); if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) { DataInputStream in = new DataInputStream(request.getInputStream()); int formDataLength = request.getContentLength(); byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; while (totalBytesRead < formDataLength) { byteRead = in.read(dataBytes, totalBytesRead,formDataLength); totalBytesRead += byteRead; } String file = new String(dataBytes); String saveFile = file.substring(file.indexOf("filename=\"") + 10); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\"")); out.println(saveFile); int lastIndex = contentType.lastIndexOf("="); String boundary = contentType.substring(lastIndex + 1,contentType.length()); int pos; 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; saveFile="C:/UploadedFiles/"+saveFile; 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> <% Connection connection = null; String connectionURL = "jdbc:mysql://192.168.10.112:3306/test"; ResultSet rs = null; PreparedStatement psmnt = null; FileInputStream fis; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); connection = DriverManager.getConnection(connectionURL, "root", "root"); File f = new File(saveFile); psmnt = connection.prepareStatement("insert into file(file_data) values(?)"); fis = new FileInputStream(f); psmnt.setBinaryStream(1, (InputStream)fis, (int)(f.length())); int s = psmnt.executeUpdate(); if(s>0) { System.out.println("Uploaded successfully !"); } else { System.out.println("unsucessfull to upload file."); } } catch(Exception e){e.printStackTrace();} } %>
thank u......