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



Home Answers Viewqa JSP-Servlet convert a file .doc to .zip
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Venkata Naga Jyothi Vure
convert a file .doc to .zip
2 Answer(s)      4 years ago
Posted in : JSP-Servlet


hiii, I uploaded a file with extension .doc now i required with .zip


Advertisement
View Answers

January 4, 2011 at 3:55 PM


Hi Friend,

Try the following code:

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=
"uploadandstore.jsp" METHOD=POST>
<br><br><br>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center>
<td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
</tr>
<tr>
<td colspan="2" align="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" align="center"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Send File"> </td>
</tr>
<table>
</center>
</FORM>
</BODY>
</HTML>

2)uploadandstore.jsp:

<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.zip.*"%>
<%
String saveFile="";
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);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
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:/"+saveFile;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br><table border="2"><tr><td><b>You have successfully upload the file:</b>
<% out.println(saveFile);%></td></tr></table>
<%

    String filesToZip = saveFile;
    File f = new File(filesToZip);
  String zipFileName="";
  int index = f.getName().lastIndexOf('.');
      if (index>0&& index <= f.getName().length() - 2 ) {
      zipFileName=f.getName().substring(0, index);
      }
  System.out.println(zipFileName);
    zipFileName = zipFileName + ".zip";

    byte[] buffer = new byte[18024];
    try{
      ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("C:/"+zipFileName));
      zos.setLevel(Deflater.DEFAULT_COMPRESSION);
      FileInputStream fis = new FileInputStream(filesToZip);
      zos.putNextEntry(new ZipEntry(filesToZip));
      int len;
      while ((len = fis.read(buffer)) > 0){
        zos.write(buffer, 0, len);
      }
      zos.closeEntry();
      fis.close();
      zos.close();
    }
    catch(Exception e){
        System.out.println(e);
    }
}

  %>

Thanks



January 4, 2011 at 3:55 PM


Hi Friend,

Try the following code:

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=
"uploadandstore.jsp" METHOD=POST>
<br><br><br>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center>
<td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
</tr>
<tr>
<td colspan="2" align="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" align="center"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Send File"> </td>
</tr>
<table>
</center>
</FORM>
</BODY>
</HTML>

2)uploadandstore.jsp:

<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.zip.*"%>
<%
String saveFile="";
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);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
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:/"+saveFile;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br><table border="2"><tr><td><b>You have successfully upload the file:</b>
<% out.println(saveFile);%></td></tr></table>
<%

    String filesToZip = saveFile;
    File f = new File(filesToZip);
  String zipFileName="";
  int index = f.getName().lastIndexOf('.');
      if (index>0&& index <= f.getName().length() - 2 ) {
      zipFileName=f.getName().substring(0, index);
      }
  System.out.println(zipFileName);
    zipFileName = zipFileName + ".zip";

    byte[] buffer = new byte[18024];
    try{
      ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("C:/"+zipFileName));
      zos.setLevel(Deflater.DEFAULT_COMPRESSION);
      FileInputStream fis = new FileInputStream(filesToZip);
      zos.putNextEntry(new ZipEntry(filesToZip));
      int len;
      while ((len = fis.read(buffer)) > 0){
        zos.write(buffer, 0, len);
      }
      zos.closeEntry();
      fis.close();
      zos.close();
    }
    catch(Exception e){
        System.out.println(e);
    }
}

  %>

Thanks



Related Tutorials/Questions & Answers:
convert a file .doc to .zip
convert a file .doc to .zip  hiii, I uploaded a file with extension .doc now i required with .zip   Hi Friend, Try the following code...;TITLE>Display file upload form to the user</TITLE></HEAD> <
convert zip file to byte array
convert zip file to byte array  Hi,How to convert zip file to byte array,can you please provide program?? Thanks, Ramanuja
Advertisements
how to convert doc file into html using java swing
how to convert doc file into html using java swing  i want to convert doc file into html and display it on jtextarea please help me and give the sample code
Convesion of txt file to doc file.??????
Convesion of txt file to doc file.??????  how to convert text file to doc file using java
doc file - Java Beginners
doc file   i want to read .doc file ?....it is a part of my project plz help. if possible , can anyone give me the code
Write data in doc file
Write data in doc file  How to write data or string in word file
read doc and docx file in javascript
read doc and docx file in javascript  How i read doc and docx file in javascript
Reading .doc file using swing
Reading .doc file using swing  Sir, Could U Please Tell me the Way To Read .doc file in java using swing,with code
sending a zip file to servlet
sending a zip file to servlet  I have created a .zip file in a servlet on the local system(no static ip). The .zip file contains xml files. I have to send it to another servlet which is in a server(has a static ip). I have done
How to zip a file using javascrip?
How to zip a file using javascrip?  hi.. can anyone tell me How to zip a file using javascrip
Create a new zip file in ZipArchive.
Creating a new Zip file in ZipArchive For creating zip file, use zipArchive () class. Create a new zip file in ZipArchive. Use the conditional statement... will generate a new zip file, in case of failure, it will print failed in creating
How to convert multiple files in java to .zip format
How to convert multiple files in java to .zip format  i receive...); } i want to convert the data from inputStream into Zip Stream and so...(); } out.close(); fout.close(); System.out.println("Zip File is created
Convert Zip To EXE
Convert Zip To EXE       In this example we are going to convert a zipped file into exe. To convert a Zip file into .exe file pass the name of a zip file using
Java file zip
Java file zip In this section, you will learn how to create a zip file. The ZIP file format is used for the distribution and storage of files. It is a compressed format file which takes less space then the original file. The zip file
zip file upload in php - WebSevices
zip file upload in php  This is not a normal upload. we know the code for normal upload. i need the zip file upload code in php. how to upload zipfile using php? and how to unzip using php? please i dont
Read file zip in J2ME - MobileApplications
Read file zip in J2ME  I would like to read file.zip in J2ME,I don't know how to extract it. Please help me
serch document from .doc and .docx file
serch document from .doc and .docx file  how to find particular word from .doc and .docx file and that date insert into a data base
openning the pdf or doc file in same jsp
openning the pdf or doc file in same jsp   **how to open the pdf and doc file in same jsp after clicking the link or button (which is with out moving to the next page). will some body help me on this @simple format please
Unzip a ZIP File
Unzip a ZIP File       How to unzip (extract) a zip file? How to retrieve elements from a zip format file? All these type of questions are solved through
How to read the .doc/ .docx file in Java Program
How to read the .doc/ .docx file in Java Program  Hi, I am beginner in Java programming language. Can anybody explain How to read .doc file in Java.... This way you can read the .doc or .docx file in Java programming language. Thanks
Creating a ZIP file in Java
Creating a ZIP file in Java       Zip File: Zip file format is the popular method of data compression. Zip file contains many files in compressed format. You can
Issue in Converting .zip file to .jar file?
Issue in Converting .zip file to .jar file?  I have a folder called "printpdf.jar". I have changed the .jar to .zip and did some customizations. After which i had "zipped" it again and made as ".jar". Now the final file
Create zip file in Java
Create zip file in Java       Introduction In this section, you will know about a zip file. You will also learn how to create a zip file from any file through the java
how to store/retrieve doc file - Java Beginners
how to store/retrieve doc file  i want to wirte a code that stores/ retrieves .doc files to the mysql database using jsp pages... can anyone help me with the code?  Use this stuff inside your jsp page for store file
How to read a line from a doc file
How to read a line from a doc file  How to read a line from a doc file   Hi Friend, To run the following code, you need to download POI... void main(String[] args) { File file = null; WordExtractor extractor = null
How to extract zip file in java.
How to extract zip file in java. In this Example, we will discuss how to extract a ZIP file. We will use ZipInputStream class for creating input stream that take input from ZIP file. The ZipInputStream class is available
Where to download spring framework zip file?
Where to download spring framework zip file?  Hi, I am trying to find the link to download the full distribution zip file of the Spring Framework.... Can anyone tell me the url from where I can download the full zip file
Read a zip file.
in reading a zip file. First select a file to be opened. Name that file into zip file and direct the zipread command to read it through while loop. The zip_entry..., it will executed and file will be closed.  <?php $zipname = zip_open("
Merge multiple jasper file to one word Doc using java
Merge multiple jasper file to one word Doc using java   how to Merge multiple jasper file to one word Doc using java
How to create a zip file and how to compare zip size with normal text file
How to create a zip file and how to compare zip size with normal text file .../ZipCreateExample.shtml but when i verified the size of zip and normal file,normal file is showing as 12 bytes and zip file was showing 150bytes in Dos prompt.Is there any error
find zip file length lessthen 5MB
find zip file length lessthen 5MB  import java.io.BufferedReader...(); } else{ System.out.println("zip file...; } } System.out.println("Zip File
Advertisements
 

 

 

DMCA.com