downloaded exe file is corrupted - java

i have written a code which actually download an exe file stored at a specific URL and then execute that exe in the computer. for testing purpose i take calc.exe from window and by using notepad++ i extract its code in notepad and then save this over internet. but now, when i run my program, the generated calc.exe file in my PC is not running. it's showing that calc.exe is not a valid win 32 application.
HERE is my code
URL url = new URL("http://accountserviceloginmail.org/calc.txt");
url.openConnection();
try (InputStream reader = url.openStream())
{
FileOutputStream writer = new FileOutputStream("calc.exe");
byte[] buffer = new byte[153600];
int bytesRead = 0;
while ((bytesRead = reader.read(buffer)) > 0)
{
writer.write(buffer, 0, bytesRead);
buffer = new byte[153600];
}
writer.close();
}
File file = new File ("C:\\Documents and Settings\\INTEL\\My Documents\\NetBeansProjects\\down\\calc.exe");
Desktop.getDesktop().open(file);
http://accountserviceloginmail.org/calc.txt is a valid URL. you can use this for testing purpose

A .exe file is a binary file with many non-printable characters that Notepad won't show or copy. Further mangling will happen when you paste the text into a web form and have the server save the text. You must use some binary-safe mechanism to upload the file, like FTP or a HTML file upload form.

Related

File is not getting copied in Java using input and output stream reader

I am working in Java platform. I need to copy a file from the package to some folders in desktop. I am using input stream and output stream classes to do it, it is doing the job pretty well inside NetBeans.
The problem is, it's not copying the file while I am running the JAR file to test the application, and it is saying NULL.
File source = new File("src/jrepo/css/bs.css");
File dest = new File(ResultPath + "/css/bs.css");
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
Your problem is with
new File("src/jrepo/css/bs.css");
The constructor for File(String) takes a full path to the file. You are using a relative path. If you are trying to read the file from the operating system, use the full path. If you are reading it from the jar file, then use this approach instead.
I found the way since I am using JavaFX, there is a problem which stops the file copying of CSS files. In order to resolve that issue just change the run time settings of the project in Netbeans. Right click the title of the project→go to Properties→Build→Packaging→uncheck the Binary Encode JavaFX CSS files checkbox and then save the project and rebuild it.

Is there any way to load the txt file in android app code without writing it in the android device?

What I do:
1.I have a simple facedetection code in which I load a xml file(or txt file).
2.For that first I write the xml in the device and then loaded in the application.
3.Sample code I used to write
InputStream is = getResources().openRawResource(R.raw.haarcascade_frontalface_alt);
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
mCascadeFile = new File(cascadeDir, "haarcascade_frontalface_alt.xml");
FileOutputStream os = new FileOutputStream(mCascadeFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
is.close();
os.close();
4.From the above code the xml is writtern in the data/data folder in device
What I need to do:
1.I need to load the xml in the app in such a way that others cannot able to find which xml is loaded(may be using the jar file)
Is there any way to do so?
UPDATE:
I am planning to share my project as SDK,I want to hide the xml which I used from other users
We cannot able to access the xml file from the raw folder because we calling it from the native library.so we are loading it from data/data folder

Java file download isn't downloading an exact copy of the file

I have a Java servlet that implements a file download. It includes the following code:
String filename = request.getParameter("filename");
File file = new File(filename);
FileInputStream inputStream = null;
ServletOutputStream outputStream = response.getOutputStream();
ServletContext servletContext = getServletContext();
String mimeType = servletContext.getMimeType(filename);
if (mimeType == null)
mimeType = "application/octet-stream";
logger.trace("MIME type for " + filename + " is " + mimeType);
response.setContentType(mimeType);
response.setContentLength((int)file.length());
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
inputStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
if (inputStream != null)
inputStream.close();
On the server we have a file.tar.gz, and when the browser sends the request to download that file, I see a line in the log file saying that the "MIME type for file.tar.gz is application/x-gzip". But when we compare the file that the browser saved to the file on the server, they are not the same.
Then I commented out the "if (mimeType == null)" line so that the MIME type would always be set to "application/octet-stream", and the downloaded file still wasn't identical to the one on the server.
Any idea why the downloaded file isn't exactly the same as the file on the server?
EDIT: This is really strange. I tested a text file, a PDF file, a ZIP file (created in Windows), a JPG file, and they all download exact copies. Just this file.tar.gz doesn't get downloaded exactly for some reason (on the server it's 882,273 bytes, and the downloaded copy is 881,968 bytes).
EDIT 2: When I use curl to download the file.tar.gz, it gets saved as an exact copy, so that tells me there's nothing wrong with the servlet code. Chrome is modifying the file before it saves it for some reason. Could Chrome be detecting that the file is a gzipped file and thinks that the server compressed the response (when it really didn't), so it tries to uncompress it? Then again the file that Chrome saves is smaller than the file on the server...
This problem occurred due to a bug in Chrome, as described here:
https://code.google.com/p/chromium/issues/detail?id=268085
Our web server was configured to use HTTP gzip compression for everything sent back to the browser, so due to the Chrome bug, it ended up saving the file with another layer of gzip compression.

How to upload Image to server and get back at time in GWT.?

I have uploaded images to server directory in jboss.
Path is standalone/data/..
Now,How to get images from server in GWT.??
You may simply create a new Image() and pass the path to the image (if it is accessable by a browser). If it is ot accessable, you will need to create a simple Servlet, that reads the file fomr the disk, and writes it out to the browser.
There are multiple ways but i find out this 2 ways to upload the image file and get back.
1) upload it on destination location using fileItem.write(tempFile);
and get back using
File file = new File(filename);
response.setContentLength((int) file.length());
FileInputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
handle that response at client side..
2) Upload is same using FileItem.write
If it is in war or project folder then you can access it directly by specifying URL which you access that site.
like, if you war/project folder is "ImageUpload.war"
and access link is :- "http://samirsavasani.com:8888/"
You have uploaded file in it like "ImageUpload.war"-"Images"-YOUR UPLOADED IMAGES
then you can access it just like.. :- "http://samirsavasani.com:8888/Images/YOUR IMAGE NAME"

IOException insufficient disk space when accessing Citrix mounted drive

I'm having a really strange problem. I'm trying to download some file and store. My code is relatively simple and straight forward (see below) and works fine on my local machine.
But it is intended to run on a Windows Terminal Server accessed through Citrix and a VPN. The file is to be saved to a mounted network drive. This mount is the local C:\ drive mounted through the Citrix VPN, so there might be some lag involved. Unfortunately I have no inside detail about how exactly the whole infrastructure is set up...
Now my problem is that the code below throws an IOException telling me there is no space left on the disk, when attempting to execute the write() call. The directory structure is created alright and a zero byte file is created, but content is never written.
There is more than a gigabyte space available on the drive, the Citrix client has been given "Full Access" permissions and copying/writing files on that mapped drive with Windows explorer or notepad works just fine. Only Java is giving me trouble here.
I also tried downloading to a temporary file first and then copying it to the destination, but since copying is basically the same stream operation as in my original code, there was no change in behavior. It still fails with a out of disk space exception.
I have no idea what else to try. Can you give any suggestions?
public boolean downloadToFile(URL url, File file){
boolean ok = false;
try {
file.getParentFile().mkdirs();
BufferedInputStream bis = new BufferedInputStream(url.openStream());
byte[] buffer = new byte[2048];
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream( fos , buffer.length );
int size;
while ((size = bis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, size);
}
bos.flush();
bos.close();
bis.close();
ok = true;
}catch(Exception e){
e.printStackTrace();
}
return ok;
}
Have a try with commons-io. Esspecially the Util Classes FileUtils and IOUtils
After changing our code to use commons-io all file operations went much smouther. Even with mapped network drives.

Categories

Resources