Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet uploading problem

Have Programming Question? Ask it here!
 
 
 


nidhin p
uploading problem
2 Answer(s)      a year and 11 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
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...


Advertisement
View Answers

August 13, 2012 at 4:07 PM


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();}
}
%>


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
Advertisements
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 "http://192.168.12.5:8080/tomcat-docs/myapps",then it is giving the following error message "http://192.168.12.5:8080/tomcat-docs/myapps
uploading audio file iphone
uploading audio file iphone  uploading audio file iphone
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
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
uploading and reading the excel daa
uploading and reading the excel daa  uploading the excel file not getting the data of the excel
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
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 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 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
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
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 file to tomcat server
uploading file to tomcat server  please tell me how to upload a file to the URL "http://192.168.12.7:8000/tomcat-docs/" ? thanks
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 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
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
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 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
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
excel uploading in jsp
excel uploading in jsp  could you provide the source code for: 1)have to upload an empty excel sheet at client side i.e if client clicks an excel icon an empty excel sheet should open 2)when they fill data in that sheet and click
video uploading using jsp
video uploading using jsp  how to upload a videos in web page using jsp   Hi, You can upload your video with the help of JSP file upload code. Once file is upload you can play using any video player. Get the code
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; } %> <
Uploading an image - JSP-Servlet
Uploading an image  I am doing a jsp project. In this uploading an image is a part. For that i had done a coding for uploading... and uploading is done by different jsp coding. I don't know how to code for file size
uploading image in the form
uploading image in the form  Hi All, I am working to build a form like railway registration form which accepts user id and password and image.../DisplayimageonJSPpageusingXML.shtml To know about file uploading using Struts2 you may go through the link, given
PHP error uploading file - PHP
PHP error uploading file  I am getting error while uploading a file in a folder in PHP ... Warning: Cannot modify header information - headers already send any idea
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
video uploading using jsp
video uploading using jsp  this is the code i hv written for uploading..but the value in dropdown list is not captured..can any 1 tell the reason...; charset=ISO-8859-1"> <title>uploading page</title> </head>
uploading a text file into a database
uploading a text file into a database  how to upload a text file into a database using jchooser   import java.io.*; import java.sql.*; import javax.swing.*; import java.awt.event.*; public class UploadTextFile
file uploading using jsp
file uploading using jsp  below file uploading code has one error "toBytes[i-start]=fromBytes; " incompatable type error please help me how to solve... in uploading "); } %>   Hi Friend, Try the following code: 1
file uploading - JSP-Servlet
problem. Im not geeting the full output for the program. Even, the file is not uploaded to the server folder. Im not sure which part is having problem. Again, i've... to solve the problem as well. Thanks in advance. Input File
file Uploading - Development process
file Uploading  Hi all, This is the problem i am facing please help me and solve the problem.i want upload file from my localpc(computer)to any textarea in webapplication.when i drop the mouse on text area the file content
file uploading - JavaMail
file uploading  Hi thi is swathi.Thank s for giving the answers to previous questions.I am getting the problem with below code FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new
file uploading - Java Server Faces Questions
questions.Previously u helped me in file uploading but is not working the description of problem is below: The file uploading code is working in lan, but in server its not working.I think the problem is www.godaddy.com didnt give any
Uploading and download pdf or .txt file.
Uploading and download pdf or .txt file.  I want admin user to upload pdf file into database and the users can download those pdf format from database
How To Write the FORM Tag Correctly for Uploading Files?
How To Write the FORM Tag Correctly for Uploading Files?  How To Write the FORM Tag Correctly for Uploading Files
image uploading perminssion in server - JSP-Servlet
image uploading perminssion in server  Dear All, I am facing some problem image uploading in server that i working fine in my system, OS Ubuntu...; Hi friend, Plz give full source code with details to solve the problem
Uploading an image into the table - JSP-Servlet
Uploading an image into the table  how to upload an image into the table in java  Hi friend, Code to help in solving the problem : import java.sql.*; import java.io.*; class SaveImageToDatabase { public
Uploading Multiple Files Using Jsp
Uploading Multiple Files Using Jsp  ...*. These are the classes which have been provided to us by the apache to help us in file uploading.... If there is a request for uploading the file then it makes a object of DiskFileItemFactory class
Uploading a software and displaying it on the jsp page
Uploading a software and displaying it on the jsp page  I have a Downloads page in my website, I want it to display the name of softwares as soon as it is uploaded in the backend by the administrator. And the admin may be a non
Uploading a single file by using JSp
Uploading a single file by using JSp  u have said about submit button..but in program u have not used submit button..and where file will be stored..where should we specify the output folder name..   Visit Here
Uploading tha file - JSP-Servlet
Uploading tha file  hi,Sir. this is siddaiah. please give me response for the follwing questions. 1.how to upload file into oracle database using JSP? 2.How to upload photo into oracle database using JSP? Regards
Advertisements
 

 

 

DMCA.com