Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Converting from bytes to String

Reply
Thread Tools

Converting from bytes to String

 
 
ramakrishna
Guest
Posts: n/a
 
      09-27-2006
Hi,
I have String object and using decodingservice i am decoding service i
am decoding the string.
DecodingService will take the bytes and return the bytes.Return is zip
files. I am converting the bytes into String object using new String(
bytes[] bytes) constructor.Result String iam storing in response object
and content type is the "application/zip".while downloading the zip
file and open with it zip file, it is showing that zip file is missing
95 bytes.

I feel that it because of converting zip bytes to String object and
storing to response object.

while iam storing bytes directly into response object it is working
nice.

Please help me to find out the reason for missing 95 bytes.

Thanks In Advance

 
Reply With Quote
 
 
 
 
Dan Andrews
Guest
Posts: n/a
 
      09-27-2006
Hi Ram,

Below is a very simple JSP file that I currently use on a server
running the 1.4 JDK. I write the bytes exactly as I read them. I have
not found any problems with it. You will want to replace the terms, the
file, and the Locator parts with what you need. If you don't want the
JSP implementation, then you might be able to translate the content
type and header parts to your implementation to see if that helps. You
might also want to remove the sleep I have. If you find any browser
problems or flaws or can suggest improvements, then I'd appreciate the
feedback.

<%

String terms = request.getParameter("TERMS");
String file = request.getParameter("FILE");

if( terms != null && terms.equals( "AGREE" ) && file != null ){
try {
String filename = ca.ansir.util.Locator.getDocumentBase() +
"downloads" + java.io.File.separator + file;

// set the http content type to "application/x-unknown"
response.setContentType("application/x-unknown");

java.io.File fileToDownload = new java.io.File(filename);

// initialize the http content-disposition header to
// indicate a file attachment with the filename
response.setHeader("Content-disposition", "attachment; filename=\""
+ file + "\"");
response.setContentLength((int)fileToDownload.leng th());
java.io.FileInputStream fileInputStream = new
java.io.FileInputStream(fileToDownload);

// flush the header
out.flush();

// transfer the file byte-by-byte to the response object
byte[] bytes = new byte[16384];
for( int count = fileInputStream.read(bytes), read = 0; count >=0;
count = fileInputStream.read(bytes)) {
response.getOutputStream().write(bytes, 0, count);
read += count;
// tune download down a bit
if( read >= 1024 * 100 ){
read = 0;
Thread.sleep( 100 );
}
}
fileInputStream.close();
out.close();
}catch(Exception e) // file IO errors
{
out.println( e.getMessage());
out.close();
e.printStackTrace();
}
} else {
%>
[snip]
<!-- Show no file selected message to user here -->
[snip]
<%
}
%>

--
Cheers,

Dan Andrews
- - - - - - - - - - - - - - - - - - - - - - - - -
Ansir Development Limited http://www.ansir.ca
- - - - - - - - - - - - - - - - - - - - - - - - -

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ratio of Bytes Delayed to Bytes Sent netproj Cisco 0 12-21-2005 08:08 PM
Converting a String object to individual bytes Erinys Javascript 1 11-16-2004 05:38 PM
Private Bytes vs. # Bytes in all Heaps in Perfmon Jason Collins ASP .Net 3 02-18-2004 03:59 PM
ASP.NET to ASP help converting bytes (xml adf format) to string Girish ASP General 1 02-16-2004 12:17 AM
Re: receiving Bytes and sending Bytes The Old Sourdough Computer Support 0 07-23-2003 01:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57