aspose file tools*
The moose likes Applets and the fly likes zipping problem in applet very urgent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "zipping problem in applet very urgent" Watch "zipping problem in applet very urgent" New topic
Author

zipping problem in applet very urgent

Rishi Yagnik
Ranch Hand

Joined: Jan 04, 2001
Posts: 84
hi friends
i am using this applet for zipping A:// files and it will get zipped in and uploaded to my server.my files are getting uploadd but when i want to unzip those files with winzip it on server is saying it is not a valid archive.can some body pls correct my code and tell me what is wromg in my code?
//Title: Global Web
//Version:
//Copyright: Copyright (c) 1999
//Author: Nitin Phadnis
//Company: Waterford Institute
//Description: Your description
package org.waterford.globalweb.presentation;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.zip.*;
public class GetData extends Applet implements ActionListener
{
boolean isStandalone = false;
private Panel labelPanel=new Panel();
private Panel choicePanel=new Panel();
private Panel drivePanel=new Panel();
private Panel butPanel=new Panel();
private Label label=new Label("Insert WCART Disk for Data Upload.");
private Label diskLabel=new Label("Select Disk :");
private Button upload=new Button("Upload Data");
private Choice drive=new Choice();
private TextArea status=new TextArea(10,7);
public static final int BUFFER_SIZE=1024512;
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public GetData() {
}
//Initialize the applet
public void init() {
System.out.println("init");
System.out.println("os.name="+System.getProperty("os.name","not known"));
System.out.println("os.arch="+System.getProperty("os.arch","not known"));
System.out.println("os.version="+System.getProperty("os.version","not known"));
System.out.println("java.class.version="+System.getProperty("java.class.version","not known"));
System.out.println("java.vendor="+System.getProperty("java.vendor","not known"));
System.out.println("java.vendor.url="+System.getProperty("java.vendor.url","not known"));
System.out.println("java.version="+System.getProperty("java.version","not known"));
try
{
System.out.println("java.class.path="+System.getProperty("java.class.path","not known"));
System.out.println("java.home="+System.getProperty("java.home","not known"));
System.out.println("file.separator="+System.getProperty("file.separator","not known"));
System.out.println("line.separator="+System.getProperty("line.separator","not known"));
System.out.println("path.separator="+System.getProperty("path.separator","not known"));
}
catch(Exception e)
{
status.setText("The Browser will not allow me to access your hard disk. Install a Plugin and Try again");
return;
}
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception
{
this.setLayout(new BorderLayout());
this.setBackground(Color.white);
this.setSize(new Dimension(450,300));
labelPanel.setBackground(Color.white);
choicePanel.setBackground(Color.white);
choicePanel.setLayout(new BorderLayout());
butPanel.setBackground(Color.white);
drivePanel.setBackground(Color.white);
status.setBackground(Color.white);
status.append("Select the Drive and click the upload button.");
status.setForeground(Color.black);
status.setEditable(false);
label.setFont(new Font("TimesNewRoman",Font.BOLD,15));
drive.add("A://");
drive.add("B://");
drive.addItemListener(new ItemListener ()
{
public void itemStateChanged(ItemEvent ie)
{
upload.setEnabled(true);
}
});
upload.addActionListener(this);
labelPanel.add(label);
drivePanel.add(diskLabel);
drivePanel.add(drive);
choicePanel.add(drivePanel,BorderLayout.NORTH);
choicePanel.add(status,BorderLayout.CENTER);
butPanel.add(upload);
this.add(labelPanel,BorderLayout.NORTH);
this.add(choicePanel,BorderLayout.CENTER);
this.add(butPanel,BorderLayout.SOUTH);
}
//Start the applet
public void start()
{
}
//Stop the applet
public void stop()
{
}
//Destroy the applet
public void destroy()
{
choicePanel.setVisible(false);
butPanel.setVisible(false);
labelPanel.setVisible(false);
}
//Get Applet information
public String getAppletInfo()
{
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo()
{
return null;
}
//Main method
public static void main(String[] args) {
GetData applet = new GetData();
applet.isStandalone = true;
Frame frame;
frame = new Frame() {
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
this.dispose();
System.exit(0);
}
}
public synchronized void setTitle(String title) {
super.setTitle(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(700,430);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Upload Data"))
getFiles();
else
return;
}
/**
* this method is used for getting a files
*/
public void getFiles()
{
String selectedDrive=drive.getSelectedItem();
File dir=new File(selectedDrive);
File[] files=dir.listFiles();
if(files==null)
{
status.append("\nEmpty Disk Found OR Select a Proper Drive for data "
+"upload.");
upload.setEnabled(false);
return;
}
else
{
for(int i=0;i<files.length;i++)
{
if((files[i].getName().equalsIgnoreCase("SM") && files[i].isDirectory())
| | (files[i].getName().equalsIgnoreCase("ITEMDIFF") &&
files[i].isDirectory()) | | (files[i].getName().equalsIgnoreCase("RESULTS")
&& files[i].isDirectory()))
{
sendZipFile(files[i]);
}
else
{
status.append("\nDisk is not a WCART Disk."+
"\nPlease insert a WCART Disk.");
return;
}
}
}
}
/**
* making of zipfile
* @param File,String
*/
public void sendZipFile(File f)
{
String fileName=f.getName();
fileName="wcart"+fileName.toLowerCase()+".zip";
try
{
status.append("\nAttempting a upload of "+fileName);
File[] fileList=f.listFiles();
if(fileList==null)
{
return;
}
status.append("\nCreate URL="+getCodeBase()+"/FormUpload.po");
if(!("http".equals(getCodeBase().getProtocol())))
{
status.append("\nRequired Protocoal not found.");
return ;
}
URL servlet=new URL(getCodeBase(),"/FormUpload.po");
status.append("\n"+servlet.toString());
URLConnection conn=servlet.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-type","multipart/form-data;boundary="+
getBoundary());
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes("\r\n" + "--" +getBoundary() + "\r\n");
out.writeBytes("content-disposition: form-data; name=\"appletfile\";filename=\""
+ fileName + "\"\r\n");
out.writeBytes("content-type: application/x-zip-compressed" + "\r\n\r\n");
ZipOutputStream zout=new ZipOutputStream(out);
try
{
for(int i=0;i<fileList.length;i++)
{
System.out.println("Files are :"+fileList[i]);
sendFile(fileList[i],zout);
}
}
finally
{
try
{
String s;
out.writeBytes("\r\n"+"--"+getBoundary()+"\r\n") ;
System.out.println("DataOutputStream size is :"+out.size());
zout.close();
out.close();
/*
BufferedReader br=new BufferedReader
(new InputStreamReader(conn.getInputStream()));
while((s=br.readLine())!=null)
status.append("\n"+s);
*/
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
catch (Exception ex)
{
System.out.println("Exception Occured :"+ex.getMessage());
ex.printStackTrace();
}
}
/**
* sending of zipfile
* @param File
*/
public void sendFile(File f,ZipOutputStream zout)
{
byte[] buffer=new byte[BUFFER_SIZE];
try
{
try
{
if(f.isDirectory())
{
File[] fileList=f.listFiles();
for(int i=0;i<fileList.length;i++)
{
sendFile(fileList[i],zout);
}
}
else
{
int n;
FileInputStream fin=new FileInputStream(f);
String absolutePath=f.getAbsolutePath().substring(2);
ZipEntry zipEntry=new ZipEntry(f.getAbsolutePath().substring(2));
zout.putNextEntry(zipEntry);
while((n=fin.read(buffer,0,buffer.length))!=-1)
{
zout.write(buffer,0,n);
}
zout.closeEntry();
}
}
catch(Exception ex){ex.printStackTrace();}
}
catch(Exception ex)
{
System.out.println("Exception Occured :"+ex.getMessage());
ex.printStackTrace();
}
}
/**
* method defined for boundry
*/
private String getBoundary()
{
return "----------------RishiPravinkumarDalpatramYagnikRajPravinkumar"
+"DalpatramYagnikPravinkumarDalpatramYagnik----------------------------";
}
}
it is very urgent.
Rishi
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

Make sure your servlet isn't using a text reader to pull the data in - ZIP files are binary!


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: zipping problem in applet very urgent
 
Similar Threads
SecurityException when running applet in IE5
browser can't be loaded error
multiple file upload in Strut2
Uploading files using applet to server
Not able to send a mail using JAVA code