uploading problem


uploading problem

nidhin p
uploading problem
2 Answer(s)      5 years and 9 months ago
Posted in : JSP-Servlet

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
                          List items = null;
                          try {
                               items = upload.parseRequest(request);
                               } catch (FileUploadException e) {

                           Iterator itr = items.iterator();
                           while (itr.hasNext())
                                       FileItem item = (FileItem) itr.next();
                                       if (item.isFormField())
                                            String name = item.getFieldName();
                                            String value = item.getString();


     } else {
             try {
                  Statement st=con.createStatement();
                  String id=session.getAttribute("fileid").toString();
                   ResultSet rs=st.executeQuery("select count(*) from
filePath where fileId='"+id+"' and fileType='PDF'");

                   out.println("You can only uploaded one file with a
unique file Id..! please upload curresponding image files start new
                   %><font size="+3" color="white">
    <tr><td><form name="form1" action="closeupload.jsp" method="post" >
<button type="submit">new upload</input></form>

<TR><TD><form name="form2" action="upload12.jsp" method="post" >
        <button type="submit">continue</button></form>

                  String itemName = item.getName();
                  File savedFile = new
                  String fname=session.getAttribute("filename").toString();
       String path =(
                  String fileP = new File(path).getName();

                  int i=st.executeUpdate ("insert into
                  session.setAttribute("path1" , savedFile);
                 }} catch (Exception e) {

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...

View Answers

August 13, 2012 at 4:07 PM

Here is a jsp application to upload any file and save it to database.


<%@ page language="java" %>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>

<FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<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><INPUT NAME="file" TYPE="file"></td></tr>
<tr><td colspan="2">
<p align="right"><INPUT TYPE="submit" VALUE="Send File" ></p></td></tr>


<%@ 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("\""));
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;

FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));

%><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://";
ResultSet rs = null;
PreparedStatement psmnt = null;
FileInputStream fis;
try {
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();}

August 14, 2012 at 4:33 PM

thank u......


Related Tutorials/Questions & Answers:
uploading problem
uploading problem  i use glassfish server.. using netbeans for jsp...(); } } } } %> my problem...: firstly.... then problem solved... bt real problem is when i upload files fusing mozilla
Problem in uploading java application
Problem in uploading java application  I have uploaded my java application (folder created under webapps) using Filezilla FtpClient.Application... this problem
File Uploading Problem
File Uploading Problem  I have a file uploading code but it create problem $(document).ready(function(){ $('#upload').click(function(){ var... it gives the problem org.apache.commons.fileupload.FileUploadException
File Uploading Problem
File Uploading Problem  I have a file uploading code but it create problem $(document).ready(function(){ $('#upload').click(function(){ var... it gives the problem org.apache.commons.fileupload.FileUploadException
Problem in uploading image to to mysql database
Problem in uploading image to to mysql database  Hi, need some help here, i have a program where a user can input name, city and upload image. when... have no problem in saving the image in the folder, my problem is it can't save
Problem in uploading image to to mysql database
Problem in uploading image to to mysql database  Hi, need some help here, i have a program where a user can input name, city and upload image. when... have no problem in saving the image in the folder, my problem is it can't save
Uploading files
Uploading files  Hi, Please provide html code for my question. I need to insert the browsed files temporarily to text area while attaching more than one files during mailing
uploading a file
uploading a file  When I am trying to upload a file to another system in lan at a location "",then it is giving the following error message "
uploading audio file iphone
uploading audio file iphone  uploading audio file iphone
File uploading - JSP-Servlet
File uploading  i am using file uploading code for multiple file and aslo for single file but i am getting problem that No such file found a exception is thrown Please suggest me to solve this problem Thank & Regards
Image uploading
Image uploading  Hi,can anyone explain the following code. The code related to uploading an image file to oracle database using java servlet. CODE import java.io.*; import java.io.IOException; import
Uploading Files - Java Server Faces Questions
Uploading Files  Hello sir, I, succeeded in uploading file. But i face a problem, that i cant upload the file having size more than 1MB. So, Please give me the solution
uploading and reading the excel daa
uploading and reading the excel daa  uploading the excel file not getting the data of the excel
Uploading a .3gp file. - MobileApplications
Uploading a .3gp file.  sir, i m sending it again .i want code for java based mobile application, that can take .3GP video file and upload... level problem for u people. Isn't it?????? reply me
Uploading File on Server
Uploading File on Server  Hello, Can someone explain or suggest example. How do i uploading files on the FTP Server. Thanks
Drag and drop file uploading - Ajax
Drag and drop file uploading  Hi all, This is NageswaraRao i want file uploading feature on my web development..using drag and drop mouse functionality. Problem:I have Created one Text area when i drop the file on text area
Uploading website
Uploading your website       Uploading Web Site After developing the web... file you can use mput command. 7) After uploading you web site
Uploading a Software and storing in the database
Uploading a Software and storing in the database  I want to upload.... The coding present in the site for uploading and storing in the database... me with the code for uploading software of bigger size
File Uploading Notification
File Uploading Notification  I am uploading files in my application and i want to know how can i know or be notified when file is uploaded. is there any file uploading event there which can tell me that process is going
Uploading Multiple Image On Server?
Uploading Multiple Image On Server?  Hello sir, I am stuck with a problem of uploading multiple images on server. i have done a code which works fine for uploading single image,but it doesn't work with uploading multiple
uploading image in php - PHP
uploading image in php  I am looking for a PHP script to upload image into mysql database
uploading and copying files
uploading and copying files  how can i upload and copy one file based on the given inputs and copy it from one directory to another
Image uploading in FTP Server
Image uploading in FTP Server  I want to upload images to a ftp server every 1 hour automatically using java..please help
uploading file to tomcat server
uploading file to tomcat server  please tell me how to upload a file to the URL "" ? thanks
File uploading - Ajax
File uploading  hi friends, how to uploading the file by using "AJAX".Please send the complete source code for this application   where u want to store the file Can u specify
rename before uploading
rename before uploading  I need to change (rename)the file name/image name before uploading it so that the file gets uploaded on server with this new name
Uploading a file using UploadBean
Uploading a file using UploadBean  Dear sir, In my project i have to upload the file and use the same file for getting a values from that uploaded .xls file.I used UploadBean for uploading .For the first time when i
query related to uploading file
query related to uploading file  hello friends i am doing my project in servlet and i want to upload a file and store file in local hard drive... to save the uploading time and date in database. please help me it is urgent
Uploading image using jsp
Uploading image using jsp  how to upload image using jsp. Already i tried, But that image file does not read. It returns only -1 without reading...("GET IMAGE PROBLEM :: "+ex); ex.printStackTrace(); } return result; } %> <
Struts file uploading - Struts
Struts file uploading   Hi all, My application I am uploading files using Struts FormFile. Below is the code. NewDocumentForm... it is breaking while uploading the large file. Please let me know if you know any API
image uploading perminssion in server - JSP-Servlet
image uploading perminssion in server  thanks dear, but i am working in Linux Ubuntu, how can i set the path in server. my problem is i am not able to access the folders wherever we are uploading the files, we are not able