File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Error sending files greater than 4MB please help urgent!!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Error sending files greater than 4MB please help urgent!!!!!" Watch "Error sending files greater than 4MB please help urgent!!!!!" New topic
Author

Error sending files greater than 4MB please help urgent!!!!!

sathish
Ranch Hand

Joined: Apr 25, 2000
Posts: 33
Hi guys
Having problems sending files greater than 4MB through my program, it is not able to send it. It looks like the server receive the correct file, but the contents are not going through
Attached below is the file
Server(Testconn) and client-(Httptest)
Please help
Thanks
sathish
______________________________________________________________
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.zip.ZipFile;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class HttpTest extends Frame implements ActionListener
{
ZipFile zip = null;
java.awt.List list = null;
//The zip file name.
String filename = null;
MenuItem mOpen, mQuit;
public static void main(String args[])
{
new HttpTest();
}
public HttpTest()
{
setTitle("UnZip Tool");

setLayout(new BorderLayout(3, 3));
add(new Label("Http URL Test: Open a jar/zip file, it will visit your Servlet and go back."),
"North");
list = new java.awt.List(20, true);
add(list, "Center");
MenuBar mb = new MenuBar();
setMenuBar(mb);
Menu mFile = new Menu("File");
mb.add(mFile);
mOpen = new MenuItem ("Open", new MenuShortcut('O'));
mFile.add(mOpen);
mFile.addSeparator();
mQuit = new MenuItem("Quit", new MenuShortcut('Q'));
mFile.add(mQuit);
mOpen.addActionListener(this);
mQuit.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { quit(); }
});
setBounds(100,100, 400, 600);
show();
}
void open()
{
FileDialog f = new FileDialog(this, "Open a Zip File",
FileDialog.LOAD);
f.show();

if(f.getFile() == null)
{
return;
}
try
{
filename = new File(f.getDirectory(), f.getFile()).getAbsolutePath();
//FileInputStream fis = new FileInputStream(filename);
//ZipInputStream zis = new ZipInputStream(fis);
zip = new HTTPComu(new File(filename)).getZipFile();
list.removeAll();
ZipEntry entry;
if (zip == null) {
return;
}

Enumeration enum = zip.entries();
while(enum.hasMoreElements()) {
entry = (ZipEntry)(enum.nextElement());
if(!entry.isDirectory()) {
list.add(entry.getName());
}
}
}
catch(Exception e)
{
System.out.println("Error: Can't open zip file " + f.getFile());
e.printStackTrace();
}
}
void quit()
{
System.exit(0);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == mOpen) open();
else if (source == mQuit) quit();
}
}
class HTTPComu {
File input = null;
String outputFile = null;
public ZipFile getZipFile() {
if (outputFile == null) {
return null;
}
try {
return new ZipFile(outputFile);
} catch (Exception e) {
return null;
}


}

private String name = "Fred's test HTTP Connector!";
private Hashtable hash = new Hashtable();

public HTTPComu(File zip) {
try {
input = zip;

hash.put("hello", " helloValue ....");
hash.put("hello!", "Christmas");
/*
Properties props = new Properties();
FileInputStream in = new FileInputStream("PostTest.properties");
prps.load(in);
props.remove("URL");
*/

URL url = new URL("http://ferret:80/servlet/TestCon");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setAllowUserInteraction(false);
send(connection);
receive(connection);
connection.disconnect();
//output = input;
} catch (Exception e) {
System.out.println("Error: " + e);
e.printStackTrace();
}
}

private void send(HttpURLConnection connection) {

try {
String contentType = "application/x-www-form-upload";
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestMethod("POST");
ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
out.writeObject(name);
out.writeObject(hash);
System.out.println("Sent hash table:\n" + hash);
FileInputStream in = new FileInputStream(input);
byte[] data = getFileData(in);
in.close();
out.writeObject(data);
System.out.println("Sending finished!");
out.flush();
out.close();
} catch (Exception e) {
System.out.println("Sedning error : " + e);
}
}
private void receive(HttpURLConnection connection) throws Exception {
try {
//HttpURLConnection.HTTP_CLIENT_TIMEOUT = 4096;
System.out.println("Receiving starts!");
System.out.println("Type: " + connection.getContentType() +
"\tLength: " + connection.getContentLength() +
"\tEncoding: " + connection.getContentEncoding());

ObjectInputStream in = new ObjectInputStream(connection.getInputStream());
System.out.println("Receiving going !");
File output = File.createTempFile("http", ".zip");
outputFile = output.getPath();
output.deleteOnExit();
System.out.println("Create tmp file " + output);
FileOutputStream fileout = new FileOutputStream(output);
Object obj;
obj = in.readObject();
hash = (Hashtable)obj;
System.out.println("Returned hash table:\n" + hash);
obj = in.readObject();
byte[] data = (byte[])obj;
fileout.write(data, 0, data.length);
fileout.close();
in.close();
System.out.println("Receiving finished! " + data.length);
} catch (Exception e) {
System.out.println("Receive error: " + e);
e.printStackTrace();
}
}
public byte[] getFileData(InputStream in)
throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int size = 2048;
byte data[] = new byte[size];
int readedSize;
long count = 0;
while((readedSize = in.read(data, 0, size)) != -1) {
out.write(data, 0, readedSize);
count += readedSize;
}
System.out.println("Reading file! " + count);

out.close();

return out.toByteArray();
}
}
-----------------------------------------------------------------
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestCon extends HttpServlet {
File output = null;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
System.out.println("Do Get in the Echo!");
//doPost(req, res);
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.print("<html>");
out.print("<head><title>Hello World3</title></head>");
out.print("<body>");
out.print("<h1>Hello World" + new Date() + "</h1>");
System.out.println("TestCon doGet!");
out.print("</body></html>");

}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
ZipFile zip;
String name;
Hashtable hash;
Object obj;

try {
//ByteArrayOutputStream buffer = new ByteArrayOutputStream();
res.setContentType("text/html");
ObjectInputStream in = new ObjectInputStream(req.getInputStream());

System.out.println("TestCon doPost!");

obj = in.readObject();
name = (String) obj;

obj = in.readObject();
hash = (Hashtable) obj;

output = File.createTempFile("http", ".zip");
output.deleteOnExit();
System.out.println("Create tmp file " + output);

FileOutputStream fileout = new FileOutputStream(output);
obj = in.readObject();
byte[] data = (byte[]) obj;
fileout.write(data, 0, data.length);
System.out.println("Reading Finished! " + data.length);

fileout.close();

//in.close();
System.out.println("Name " + name);
System.out.println("Hash " + hash);
//System.out.println("Length " + length);

////Send back
//ObjectOutputStream out = new ObjectOutputStream(buffer);

res.setBufferSize(data.length + 10240);
ObjectOutputStream out = new ObjectOutputStream(res.getOutputStream());
out.writeObject(hash);
System.out.println("Start to sending back!");
//FileInputStream filein = new FileInputStream(output);
//data = getFileData(filein);
//filein.close();
out.writeObject(data);
System.out.println("Writing data!");
//res.setBufferSize(buffer.size() + 1024);
//res.setContentLength(buffer.size());
out.close();
System.out.println("Sending back finished!" + data.length);
} catch (Exception e) {
System.out.println("Reading data error " + e);
e.printStackTrace();
}
}
public byte[] getFileData(InputStream in)
throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int size = 2048;
byte data[] = new byte[size];
int readedSize;
long count = 0;
while((readedSize = in.read(data, 0, size)) != -1) {
out.write(data, 0, readedSize);
count += readedSize;
}
System.out.println("\tReading file ! " + count);

out.close();

return out.toByteArray();
}
public String getServletInfo() {
return "Echo what it gets from client";
}
}
sathish
Ranch Hand

Joined: Apr 25, 2000
Posts: 33
the problem occurs when
A(Client) ------> Establishes a HttpUrlConnection ---->B(Server)
B(Server) -----> Receives the request and sends back ----> A(Client) --->Timeout occurs and not able to send the contents..
Why?
Is that related to the file size???
Please help
Thanks
sathish
sathish
Ranch Hand

Joined: Apr 25, 2000
Posts: 33
Is any one out there to help me???
thanks
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
I think people might like to help you, but find the large amount of code you have posted somewhat daunting. Can you put together a smaller code example which demonstrates this (say 30 lines or less?) which the rest of us can easily pick up and run on our own systems to try and work out what the problem is?
Remember that everyone here is doing this on a purely voluntary basis, and there are always lots of other questions to answer. The more precise and readable your question is, the more likely someone is to want to answer it.


A Convergent Visionary ~ Frank's Punchbarrel Blog ~ LinkedIn profile
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
Please note that The Java Ranch has a naming policy, described here and "sathish" is not a valid name. Please choose one which meets the requirements.
Thanks.
asad ali
Greenhorn

Joined: Dec 12, 2000
Posts: 20
I tried your application on my PC and it works.
Following is my environment
1. Windows NT workstation 4.0
2. Using JRun 3.0 for servlet
3. Using JDK1.2.2
4. My zip file size is 4,965 KB
I commented the line
res.setBufferSize(data.length + 10240);
in your servlet's doPost method as it is not recognized method for response interface.
sathish
Ranch Hand

Joined: Apr 25, 2000
Posts: 33
Hi Asad ali
Thanks for your reply, but when you comment out the res.setbuffersize line from the doPost method, the first time it executes doesn't work, however, can you try sending files of the size 5.00MB ++?
And, if you keep sending files repeatedly, it fails to respond, and i have also submitted it to the sun developer site.
Thanks for your reply
sathish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error sending files greater than 4MB please help urgent!!!!!
 
Similar Threads
ByteArrayStream steam!
Data lost when writing object to a file
Object reference graphs containing cycles can be serialized??
How to save properly?
Why isn't my code Opening a Saved file?