uploading problem

Ads
 

uploading problem

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

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

Ads









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
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 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 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 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 "http://192.168.12.7:8000/tomcat-docs/" ? thanks
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 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; } %> <
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
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
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
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
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
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
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

Ads