Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Converting from bytes to String

Thread view: 
ramakrishna - 27 Sep 2006 08:33 GMT
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
Dan Andrews - 27 Sep 2006 15:26 GMT
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.length());
   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
- - - - - - - - - - - - - - - - - - - - - - - - -


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.