I'm receiving a File from server and in client I want to save this file to a directory. Here is the code that do the job of saving the File
FileDetails obj1= (FileDetails)object;
String str= "A "+obj1.fileExtension+" Received From "+obj1.source;
JOptionPane.showMessageDialog(null,str,"Success", WIDTH, null);
FileOutputStream saveFile = new FileOutputStream("F:\\Download\\"+obj1.fileExtension);
ObjectOutputStream save = new ObjectOutputStream(saveFile);
byte[] buf= convertToByteArray(obj1.file);
save.write(buf);
save.close();
The function convertToByteArray is as following
private byte[] convertToByteArray(File file){
try{
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); //no doubt here is 0
bos.flush();
System.out.println("read " + readNum + " bytes,");
}
byte[] bytes = bos.toByteArray();
return bytes;
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
FileDetails is a serializable Object which contains the file that server sends and the file name in fileExtension. The class is as following -
public class FileDetails implements Serializable{
public File file;
public String fileExtension;
public String source;
public String destination;
}
now problem is I get Junk Data in the saved file in client side. To test whether I'm receiving pure data from server I've printed the content of the file in console and got expected result. But when I open the saved file in the specified directory I get some Chinese scripts What can I do ? Please help me out.
Part of the problem is, I think, looking at the file in Notepad. If I open the file in another editor, say TextPad using binary mode, I see that the file starts with a serialization header. Using the ObjectOutputStream serializes the binary data of the file as an object. What I think you want to do is remove the ObjectOutputStream line and instead change it to the following:
FileOutputStream saveFile = new FileOutputStream("F:\\Download\\"+obj1.fileExtension);
//ObjectOutputStream save = new ObjectOutputStream(saveFile);
byte[] buf= convertToByteArray(obj1.file);
saveFile.write(buf);
saveFile.close();
That will write out the bytes ONLY of the file.
Take a look at the following:
http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#move(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...)
Related
is it possible to write/create an exe file in Java?
I can successfully read it but writing the exact same data that has been read to a new file seems to create some trouble because Windows tell's me it's not supported for my pc anymore.
This is the code I'm using to read the file where path is a String given with the actual path (it's in the .jar itself that's why I'm using ResourceAsStream()):
try {
InputStream inputStream = FileIO.class.getResourceAsStream(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
ArrayList<String> _final = new ArrayList<String>();
String line;
while ((line = reader.readLine()) != null) {
_final.add(line);
}
inputStream.close();
return _final.toArray(new String[_final.size()]);
}catch(Exception e) {
return null;
}
This is the code I'm using to write the file:
public static void writeFileArray(String path, String[] data) {
String filename = path;
try{
FileWriter fileWriter = new FileWriter(filename);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for(String d : data) {
bufferedWriter.write(d + "\n");
}
bufferedWriter.close();
}
catch(IOException ex){
System.out.println("FileIO failed to write file, IO exception");
}
}
So it doesn't give me any error's or something and the file size of the original .exe and the 'transferred' .exe stays the same, but it doesn't work anymore. Am I just doing it wrong? Did I forget something? Can u even do this with Java?
Btw I'm not that experienced with reading/writing files..
Thanks for considering my request.
I'm going to guess that you're using a Reader when you should be using a raw input stream. Use BufferedInputStream instead of BufferedReader.
BufferedInputStream in = new BufferedInputStream( inputStream );
The problem is that Reader interprets the binary as your local character set instead of the data you want.
Edit: if you need a bigger hint start with this. I just noticed you're using a BufferedWriter too, that won't work either.
try {
InputStream inputStream = FileIO.class.getResourceAsStream(path);
BufferedInputStream in = new BufferedInputStream( inputStream );
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] bytes = new byte[ 1024 ];
for( int length; ( length = ins.read( bytes ) ) != -1; )
bos.write( bytes, 0, length );
}
inputStream.close();
return bos;
When you are using Java 7 or newer, you should copy a resource to a file using
public static void copyResourceToFile(String resourcePath, String filePath) {
try(InputStream inputStream = FileIO.class.getResourceAsStream(resourcePath)) {
Files.copy(inputStream, Paths.get(filePath));
}
catch(IOException ex){
System.out.println("Copying failed. "+ex.getMessage());
}
}
This construct ensures correct closing of the resources even in the exceptional case and the JRE method ensures correct and efficient copying of the data.
It accepts additional options, e.g. to specify that the target file should be overwritten in case it already exists, you would use
public static void copyResourceToFile(String resourcePath, String filePath) {
try(InputStream inputStream = FileIO.class.getResourceAsStream(resourcePath)) {
Files.copy(inputStream, Paths.get(filePath), StandardCopyOption.REPLACE_EXISTING);
}
catch(IOException ex){
System.out.println("Copying failed. "+ex.getMessage());
}
}
You are using InputStreams for strings, .exe files are bytes!
Try using a ByteArrayInputStream and ByteArrayOutputStream.
Edit: completing with markspace's answer:
new BufferedInputStream(new ByteArrayInputStream( ... ) )
In an Android project I am reading an audio file into an InputStream and consequently write it to another location on the device. When reading, in.read(buffer) as shown in the snippet returns -1. After running the app, I locate the file using the phone's file manager app to play. But it doesn't play because the file is empty, i.e. size is 0 bytes. What is the right way to read and write audio files in using InputStreams and OutputStreams?
try {
DocumentFile newFile = pickedDir.createFile("audio/mp3", "New File");
OutputStream out = getContentResolver().openOutputStream(newFile.getUri());
InputStream in = new FileInputStream("/storage/emulated/0/beat.mp3");
// To check whether the source file exists
File testFile = File("/storage/emulated/0/beat.mp3");
Log.d("App", "exists: " + testFile.exists() + " len: " + testFile.length());//exists: true len: 0
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
out.flush();
out.close();
} catch (Exception e) {
Log.d("Exception ", e.getMessage());
}
to be pedantic, use:
FileInputStream in = new FileInputStream("/storage/emulated/0/beat.mp3");
And just make sure the path is correct and the file exists. Other than that, from the info you gave, I cannot think of anything else
OutputStream out = getContentResolver().openOutputStream(newFile.getUri());
InputStream in = new FileInputStream("/storage/emulated/0/beat.mp3");
File newFile = File("/storage/emulated/0/beat.mp3");
Seem your code have problem here, newFile.getUri() is called before it init?
I have an application where I am generating a "target file" based on a Java "source" class. I want to regenerate the target when the source changes. I have decided the best way to do this would be to get a byte[] of the class contents and calculate a checksum on the byte[].
I am looking for the best way to get the byte[] for a class. This byte[] would be equivalent to the contents of the compiled .class file. Using ObjectOutputStream does not work. The code below generates a byte[] that is much smaller than the byte contents of the class file.
// Incorrect function to calculate the byte[] contents of a Java class
public static final byte[] getClassContents(Class<?> myClass) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try( ObjectOutputStream stream = new ObjectOutputStream(buffer) ) {
stream.writeObject(myClass);
}
// This byte array is much smaller than the contents of the *.class file!!!
byte[] contents = buffer.toByteArray();
return contents;
}
Is there a way to get the byte[] with the identical contents of the *.class file? Calculating the checksum is the easy part, the hard part is obtaining the byte[] contents used to calculate an MD5 or CRC32 checksum.
THis is the solution that I ended up using. I don't know if it's the most efficient implementation, but the following code uses the class loader to get the location of the *.class file and reads its contents. For simplicity, I skipped buffering of the read.
// Function to obtain the byte[] contents of a Java class
public static final byte[] getClassContents(Class<?> myClass) throws IOException {
String path = myClass.getName().replace('.', '/');
String fileName = new StringBuffer(path).append(".class").toString();
URL url = myClass.getClassLoader().getResource(fileName);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try (InputStream stream = url.openConnection().getInputStream()) {
int datum = stream.read();
while( datum != -1) {
buffer.write(datum);
datum = stream.read();
}
}
return buffer.toByteArray();
}
I don't get what you means, but i think you are looking for this, MD5.
To check MD5 of a file, you can use this code
public String getMd5(File file)
{
DigestInputStream stream = null;
try
{
stream = new DigestInputStream(new FileInputStream(file), MessageDigest.getInstance("MD5"));
byte[] buffer = new byte[65536];
read = stream.read(buffer);
while (read >= 1) {
read = stream.read(buffer);
}
}
catch (Exception ignored)
{
int read;
return null;
}
return String.format("%1$032x", new Object[] { new BigInteger(1, stream.getMessageDigest().digest()) });
}
Then, you can store the md5 of a file in any way for exmaple XML. An exmaple of MD5 is 49e6d7e2967d1a471341335c49f46c6c so once the file name and size change, md5 will change. You can store md5 of each file in XML format and next time your run a code to check md5 and compare the md5 of each file in the xml file.
If you really want the contents of the .class file, you should read the contents of .class file, not the byte[] representation that is in memory. So something like
import java.io.*;
public class ReadSelf {
public static void main(String args[]) throws Exception {
Class classInstance = ReadSelf.class;
byte[] bytes = readClass(classInstance);
}
public static byte[] readClass(Class classInstance) throws Exception {
String name = classInstance.getName();
name = name.replaceAll("[.]", "/") + ".class";
System.out.println("Reading this: " + name);
File file = new File(name);
System.out.println("exists: " + file.exists());
return read(file);
}
public static byte[] read(File file) throws Exception {
byte[] data = new byte[(int)file.length()]; // can only read a file of size INT_MAX
DataInputStream inputStream =
new DataInputStream(
new BufferedInputStream(
new FileInputStream(file)));
int total = 0;
int nRead = 0;
try {
while((nRead = inputStream.read(data)) != -1) {
total += nRead;
}
}
finally {
inputStream.close();
}
System.out.println("Read " + total
+ " characters, which should match file length of "
+ file.length() + " characters");
return data;
}
}
I have an assignment where i need to create a .xlsx and convert it to base64 and this file will sent as an attachment in an email webservice.The code for conversion of .xlsx to base64 is as below:
import java.io.*;
import org.apache.commons.codec.binary.Base64;
public class Test {
public static void main(String [] args) {
String fileName = "C:/Users/kk.txt";
try {
byte[] buffer = new byte[1000];
FileInputStream inputStream =
new FileInputStream(fileName);
int total = 0;
int nRead = 0;
String reference=null;
while((nRead = inputStream.read(buffer)) != -1) {
String name=(new String(buffer));
byte[] encodedBytes = Base64.encodeBase64(name.getBytes());
System.out.println(new String(encodedBytes));
}
inputStream.close();
System.out.println("Read " + total + " bytes");
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
but when i send it as a mail using BPEL and when i open the file it shows general input/output error.But when i send it as a text it works fine. Should i not use this code for excel or where am i wrong.Am totally new to java.
Okay - my guess is that you're not changing the mimetype etc features of the email you're creating for the attachment ( and that it's default is text/html ).
Take a look here - which is for a word doc instead - but outlines what I think you should be doing ( assuming I found the right kind of BPEL )
For your file type ".xlsx", this table shows the appropriate mime-type is like
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
package com.denemeler;
import java.io.*;
import java.util.Base64;
public class test {
public static void main(String[] args) throws IOException {
String filePath = "D:\\test.xls";
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
fis.read(bytes);
String base64 = new sun.misc.BASE64Encoder().encode(bytes);
String destinationPath = "D:\\Destination\\donusmusHali.xls";
//decode Base64 String to image
FileOutputStream fos = new FileOutputStream(destinationPath);
bytes = new sun.misc.BASE64Decoder().decodeBuffer(base64);
fos.write(bytes);
fis.close();
fos.close();
}
}
this code works for encoding and decoding excel files.
Generally, we use filename.xls in outputStream to write our excel workbook.
But if need to send it over network in base64 format then ByteArrayOutputStream is the option to go for it:
Workbook workbook = new HSSFWorkbook();
ByteArrayOutputStream b = new ByteArrayOutputStream();
try {
workbook.write(b);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
byte[] bytes = bos.toByteArray(
);
Base64.encodeBase64String(bytes);
This solution worked for me, As first excel was created using Apache POI then applied above code and converted it to base64, which when decoded over network then encoded file was getting opened in a excel application as expected. :)
You can follow same on https://nupur28ag.blogspot.com/2020/01/get-base64-from-excel-created-using.html
I want to retrieve file from ftp server, I have also used Apache poi client.retrieveFile() method, but I'm unable to open it which is showing:
excel cannot open file check file extension and file format. check file is not corrupted
Then I used file reader and writer. Below is my code snippet.
public void testFileWriter()
{
try{
FTPFile[] files = client.listFiles("/Ftp1");
for (FTPFile file : files) {
File serverFile = new File("D:/Pondi.xlsx");
if(!serverFile.isFile())
{
serverFile.createNewFile();
}
BufferedWriter writer = new BufferedWriter(new FileWriter(serverFile));
client.enterLocalPassiveMode();
InputStream inputStream = client.retrieveFileStream("/Ftp1/"+ file.getName());
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
System.out.println("Created Reader");
while(reader.read()!=-1)
{
String temp = reader.readLine();
System.out.println(temp);
writer.write(temp);
}
writer.flush();
writer.close();
reader.close();
}
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
Please help me to resolve this crucial issue.
You have to use an API for working with it. You can't read these files like reading normal text files.
JExcel will be good for your need.
Examples are be available here
For copying files make use of this. Reading the file for copying by the method that you used won't work properly.
Hope will be helpful for you.
If you want to read and copy binary data, you must not use reader.readLine(), because there are no lines in a binary file. Therefore, this attempt will most likely fail.
Copy it like this instead:
int fileNo = 0;
for (FTPFile file : files) {
File serverFile = new File("D:/Pondi_" + fileNo + ".xlsx");
...
InputStream in = client.retrieveFileStream("/Ftp1/"+ file.getName());
OutputStream out = new FileOutputStream(serverFile);
// read and copy binary data
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
fileNo++;
}
Beside that, consider giving your files a different name that D:/Pondi.xlsx, because otherwise the file gets overridden again and again in the loop. I did this with fileNo.