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



Home Answers Viewqa JSP-Servlet Uploading and download pdf or .txt file.
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Prasad.G
Uploading and download pdf or .txt file.
2 Answer(s)      2 years and 9 months ago
Posted in : JSP-Servlet


I want admin user to upload pdf file into database and the users can download those pdf format from database?


Advertisement
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
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.
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
Convesion of txt file to doc file.??????
Convesion of txt file to doc file.??????  how to convert text file to doc file using java
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
Backup Into txt File
Backup Into txt File       Backup Into txt File is used to put the backup file from a ... illustrate an example from 'Backup Into txt File'. To understand this example
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
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
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
uploading audio file iphone
uploading audio file iphone  uploading audio file iphone
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
Backup selected records into txt file
Backup selected records into txt file       Backup selected records into txt file is used to copies... into txt file. To restore the selected backup records into txt file, we create
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 download
file download  I uploaded a file and saved the path in database. Now i want to download it can u plz provide code
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 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... The Tutorial illustrate an example from 'Backup Into txt 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
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
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
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
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
convert .txt file in .csv format - Java Beginners
convert .txt file in .csv format  Dear all, I hope you are doing good. I am looking to convert .txt file in .csv format. The contents might have... want export these contents to a .csv file. I am aware that I need to use
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 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
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
Remove JTable row that read txt file records
Remove JTable row that read txt file records  Hi every one. i have a jtable that correctly read data frome file and show them in own. I want to add a "Delete" button that when select a row and clicked button, row must deleted
How to import txt file using SQL
How to import txt file using SQL  Hai, I have a log file in .txt format and format of the log is given below. From the log i want to pick up some particular words and insert onto a table using SQL. I want the output of the table
Advertisements
 

 

 

DMCA.com