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?

View Answers

January 4, 2012 at 11:42 AM

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;

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/file";
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();}
}
%> 
<a href="download.jsp">Download File</a>

January 4, 2012 at 11:45 AM

continue....

3)download.jsp:

<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%@page import="javax.servlet.*"%>
<%
ServletOutputStream output = response.getOutputStream();
String connectionURL = "jdbc:mysql://192.168.10.112:3306/file";
String Content=new String("");
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection(connectionURL,"root","root");
Statement st=con.createStatement();
ResultSet rst= st.executeQuery("select * from file");
if(rst.last())
{
Content=rst.getString("file_data");
}
con.close();
}catch(Exception e){
System.out.println("Exception caught"+e.getMessage());
}
byte requestBytes[] = Content.getBytes();
ByteArrayInputStream bis = new ByteArrayInputStream(requestBytes);
response.reset();
response.setContentType("application/pdf");
response.setHeader("Content-disposition","attachment; filename=" +"file.pdf");
byte[] buf = new byte[1024];
int len;
while ((len = bis.read(buf)) > 0){
output.write(buf, 0, len);
}
bis.close();
response.getOutputStream().flush();
%>









Related Tutorials/Questions & Answers:
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
Download PDF file
Download PDF file  How to download PDF file with JSF
Advertisements
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
Thread for reading txt file
Thread for reading txt file  how to use 3 thread to read 3 txt file? To create three threads for reading the file and three threads for getting the strings out of the queue and printing them. thanks
Convert pdf to rtf and txt - XML
Convert pdf to rtf and txt  Can we convert PDF to RTF and TXT, and how?  Hi Friend, Try the following codes: 1)Convert PDF to RTF...) {} } } 2)Convert PDF to TEXT import java.io.*; import java.util.
file uploading
file uploading  How to upload music files onto the server and save the file information to the mysql database fetch and play the music files on a display page without download thus streaming the files
Convesion of txt file to doc file.??????
Convesion of txt file to doc file.??????  how to convert text file to doc file using java
uploading audio file iphone
uploading audio file iphone  uploading audio file iphone
download pdf files
download pdf files  pls help me,I don't know how to convert .doc,.docx files into pdf files and download that pdf files using servlet or jsp
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
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 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
Backup Into txt File
Backup Into txt File       Backup Into txt File is used to put the backup file from a ... The Tutorial illustrate an example from 'Backup Into txt File
Struts file uploading - Struts
Struts file uploading   Hi all, My application I am uploading... can again download the same file in future. It is working fine when I... in the database without breaking and user should be able to download the file
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
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
Download Current Web Page As PDF
Download Current Web Page As PDF  Need a utility to save current web page as pdf file into specified directory... Thanks In Adv
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 and file name and path in oracle database.with name file name and path i also want
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.... http://www.roseindia.net/jsp/file_upload/Sinle_upload.xhtml.shtml
file download
file download  I uploaded a file and saved the path in database. Now i want to download it can u plz provide code
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 it to youtube using youtube's APIs. The video file would be located on a weblink
File Uploading Problem
File Uploading Problem  I have a file uploading code but it create... = FileUpload.isMultipartContent(request); // Create a new file upload handler... { System.out.println("its a file"); System.out.println
File Uploading Problem
File Uploading Problem  I have a file uploading code but it create... = FileUpload.isMultipartContent(request); // Create a new file upload handler... { System.out.println("its a file"); System.out.println
uploading a text file into a database
uploading a text file into a database  how to upload a text file... { static File file; public static void main(String[] args) throws Exception { JLabel label=new JLabel("Choose File
file uploading using jsp
file uploading using jsp  below file uploading code has one error... = " + formDataLength); //String file = new String(dataBytes); //out.println("FileContents:" + file +""); byte[] line = new byte[128]; if (totalBytesRead
ModuleNotFoundError: No module named 'file_open_gips_txt'
ModuleNotFoundError: No module named 'file_open_gips_txt'  Hi, My... named 'file_open_gips_txt' How to remove the ModuleNotFoundError: No module named 'file_open_gips_txt' error? Thanks   Hi, In your
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
file uploading - Java Beginners
file uploading   hi, i am developing one stand alone j2se programming in that i have to upload a file to another system in the LAN and have to download from the server .i am using java.io. and java.net and swing .the task
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 - JSP-Servlet
file uploading  Hi, thanks a lot for your kind answer. Now my program... problem. Im not geeting the full output for the program. Even, the file... to solve the problem as well. Thanks in advance. Input File
FILE UPLOADING - JSP-Servlet
FILE UPLOADING  Hi , I want Simple program for file upload using html and servlet plese help me  hi friend pls try this code ********** try{ String type=""; String boundary=""; String sz
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
pdf file in jsp
pdf file in jsp  i have a pdf, which has to be displayed in a jsp page along with some html tags, the pdf which is to be displayed shold be readonly and wont allow the uer to save too
about pdf file handeling
about pdf file handeling  can i apend something in pdf file using... to handle pdf files. You can find the related examples from the given link: Handling PDF files
Backup selected records into txt file
Backup selected records into txt file       Backup selected records into txt file is used to copies... Selected records into txt file. To restore the selected backup records into txt
uploading a file at another system in lan using jsp
uploading a file at another system in lan using jsp  Thanks... to save or upload the file at another system in lan at location "http://hpws1/sharedmxd/pdf/" it gave an error message " http:\hpws1\sharedmxd\pdf\EAIMConcepts.pdf
about pdf file handeling
about pdf file handeling  i have downloaded a jar file containing packages for writing in pdf files . in which folder i should copy it ? i'm using windowsXP os.pls give whole path
Backup Into txt File
Backup Into txt File       Backup Into txt File is used to put the backup file from..._TO_REPLACE_1 The Tutorial illustrate an example from 'Backup Into txt File
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
how to change file from .txt to .mat(matrix)
how to change file from .txt to .mat(matrix)  i have a big file.txt and i want to change this file to file.mat(matrix) ...this is in windows not on any os ..thx if u answering quickly please
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
File Download Security
File Download Security   Hello, I trying to create an application in PHP to prevent or provide file download security. So, I need the help of senior... files downloading. Please read this article about File Download Security
convert data from pdf to text file - Java Beginners
convert data from pdf to text file   how to read the data from pdf file and put it into text file(.txt
Convert Text File to PDF file
Convert Text File to PDF file  Here is the way how to covert your Text file to PDF File, public class TextFileToPDF { private static void...(inLine); System.out.println("Text is inserted into pdf file
ModuleNotFoundError: No module named 'django-pdf-download'
ModuleNotFoundError: No module named 'django-pdf-download'  Hi, My... named 'django-pdf-download' How to remove the ModuleNotFoundError: No module named 'django-pdf-download' error? Thanks   Hi
Shifting txt file to database - Java Beginners
Shifting txt file to database   Question Details: I want to shift data from txt file to Database. The data is written in the following text format. Record No. = 0001 Name : Abdul Rauf Designation
Uploading the file on Server.
Uploading the file on Server This example will explain you how to upload the file on FTP server. ftp_put() command allows to upload an existing file on the server. For uploading a file onto the FTP server, first you have to login
Convert Text File to PDF file
Convert Text File to PDF file  import java.io.BufferedReader; import...); System.out.println("Text is inserted into pdf file"); document.close... FileReader( Input File)); String inLine = null
uploading a file - JSP-Interview Questions
uploading a file   uploading a file and storing in database using...; public File currentDir; public DropTarget target; JButton addButton...,BorderLayout.SOUTH); currentDir = new File(System.getProperty("user.dir")); } public

Ads