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



Home Answers Viewqa JSP-Servlet uploading problem
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


nidhin p
uploading problem
2 Answer(s)      2 years and 3 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
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
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
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
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; } %> <
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
Advertisements
 

 

 

DMCA.com