I have hosted my java web application in a unix server the files are generated in a context path,zipped and downloaded to the client. My code is working once I have hosted the Application in a Windows server but not in Centos .. What could be the issue?
My zip Method:
private byte[] zipFiles(File directory, String[] files) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
byte bytes[] = new byte[2048];
for (String fileName : files) {
FileInputStream fis = new FileInputStream(directory.getPath()
+ crbServlet.FILE_SEPARATOR + fileName);
BufferedInputStream bis = new BufferedInputStream(fis);
zos.putNextEntry(new ZipEntry(fileName));
int bytesRead;
while ((bytesRead = bis.read(bytes)) != -1) {
zos.write(bytes, 0, bytesRead);
}
zos.closeEntry();
bis.close();
fis.close();
}
zos.flush();
baos.flush();
zos.close();
baos.close();
return baos.toByteArray();
}
My servlet Extract:
String[] files = f.list();
if (files != null && files.length > 0) {
byte[] zip = zipFiles(f, files);
ServletOutputStream sos = response.getOutputStream();
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=\"datafiles" + dt.getCurrentDate() + ".zip\"");
sos.write(zip);
sos.flush();
}
What I am getting downloaded on the client side is a do file that has junk character. What is the issue here?
Related
I am getting the issue while trying to decompress the LZO file using java. Below is the code and error I have pasted, can someone please help me on this
import org.anarres.lzo.*;
import java.io.*;
public class LZODecompression {
public static void main(String args[]) throws IOException {
InputStream in = new FileInputStream(new
File("/desktop/mm_impressions_101349_20220723_2022072802.txt.lzo"));
LzoAlgorithm algorithm = LzoAlgorithm.LZO1X;
LzoDecompressor decompressor = LzoLibrary.getInstance().newDecompressor(algorithm,
null);
LzoInputStream stream = new LzoInputStream(in, decompressor);
OutputStream outputStream = new FileOutputStream(new File("/Desktop/test.txt"));
int len;
byte[] bytes = new byte[1024];
while ((len = stream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
}
outputStream.close();
stream.close();
}
}
Exception in thread "main" java.io.EOFException
at org.anarres.lzo.LzoInputStream.readBytes(LzoInputStream.java:183)
at org.anarres.lzo.LzoInputStream.readBlock(LzoInputStream.java:132)
at org.anarres.lzo.LzoInputStream.fill(LzoInputStream.java:119)
at org.anarres.lzo.LzoInputStream.read(LzoInputStream.java:102)
at org.anarres.lzo.LzoInputStream.read(LzoInputStream.java:97)
at org.example.LZODecompression.main(LZODecompression.java:37)
I have used LzopInputStream instead of LzoInputStream and the file got decompressed successfully in Linux. I used following code :
public static void decompressOriginal() throws Exception
{
String basePath = "/home/saad/CompressionTest/";
String compressedFileName = "temp1.lzo";
String afterDecompressFilename = "temp1.txt";
System.out.println("Decompressing:" + compressedFileName);
InputStream in = new FileInputStream(new File(basePath + compressedFileName));
LzoInputStream stream = new LzopInputStream(in);
OutputStream outputStream = new FileOutputStream(new File(basePath + afterDecompressFilename));
int len;
byte[] bytes = new byte[256];
while ((len = stream.read(bytes)) != -1)
{
outputStream.write(bytes, 0, len);
}
outputStream.close();
stream.close();
System.out.println("Successfully Decompressed:" + afterDecompressFilename);
}
However, I used following code to compress file:
public static void compressFile() throws IOException
{
String basePath = "/home/saad/CompressionTest/";
String sourceFile = "source1.sql";
String destinationFile = "temp1.lzo";
File plainTextFile = new File(basePath + sourceFile);
InputStream inputStream = new FileInputStream(plainTextFile);
byte[] byteArray = org.apache.commons.io.IOUtils.toByteArray(inputStream);
inputStream.close();
System.out.println("File:" + sourceFile + " Array Size:" + byteArray.length);
LzoCompressor compressor = LzoLibrary.getInstance().newCompressor(LzoAlgorithm.LZO1X, null);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
LzopOutputStream lzopOutputStream = new LzopOutputStream(outputStream, compressor, 1024 * 100, LzopConstants.F_ADLER32_C);
lzopOutputStream.write(byteArray);
lzopOutputStream.close();
System.out.println("Compression Complete:" + sourceFile);
org.apache.commons.io.FileUtils.writeByteArrayToFile(new File(basePath + destinationFile), outputStream.toByteArray());
System.out.println("Written File:" + destinationFile);
}
Because when I used system software for compression it gave following error:
Exception in thread "main" java.io.IOException: Compressed with
incompatible lzo version: 0x2080 (expected 0x2050)
Because LZO compression is updated, but I was not able to get updated version of used library.
We would need to use some other library with the support of latest LZO Version for that.
I have a file in the server, I want to create three java APIs, which will do the below three operations in dependently.
Get the file size
Move a file with different file name to a different server location
Zip the file
In my existing code we are executing Linux commands to perform those operations, unfortunately, Linux commands are not getting executed, this is due to some server/set up issue, so I am forced to use Java commands.(We use JDK 1.6)
I am not a Java developer. I have gone through some of the previously answered questions, but they are not explaining about file in server path. Any help is much appreciated.
To get the file size in bytes:
File file = new File("filename.txt");
long fileSize = file.length();
To move a file you must first copy it and then delete the original:
InputStream inStream = null;
OutputStream outStream = null;
try {
File fromFile = new File("startfolder\\filename.txt");
File toFile = new File("endfolder\\filename.txt");
inStream = new FileInputStream(fromFile);
outStream = new FileOutputStream(toFile);
byte[] buffer = new byte[1024];
int length;
while ((length = inStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
fromFile.delete();
} catch(IOException e) {
e.printStackTrace();
}
To zip a file:
byte[] buffer = new byte[1024];
try {
FileInputStream fileToZip = new FileInputStream("filename.txt");
FileOutputStream fileOutputStream = new FileOutputStream("filename.zip");
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
ZipEntry zipEntry= new ZipEntry("filename.txt");
zipOutputStream.putNextEntry(zipEntry);
int len;
while ((len = fileToZip.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, len);
}
fileToZip.close();
zipOutputStream.closeEntry();
zipOutputStream.close();
} catch(IOException e) {
e.printStackTrace();
}
I need to send the audio file the from client to the server. So I have a REST API for which the attachment is the input. I need to convert this attachment to audio file in the Java layer. I tried two different ways but both are time consuming. Any suggestions?
Method1:
File tempFile=File.createTempFile("tempFile", ".wav");
tempFile.deleteOnExit();
DataHandler handler = attachemnt.getDataHandler();
InputStream Filestream = handler.getInputStream();
if (Filestream.available()>10) {
OutputStream out = new FileOutputStream(tempFile);
byte[] buf = new byte[1024];
int len;
while ((len = Filestream.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
Filestream.close();
}
Method2:
File tempFile=File.createTempFile("tempFile", ".wav");
tempFile.deleteOnExit();
DataHandler handler = attachment.getDataHandler();
InputStream is = handler.getInputStream();
if (is.available() > 10) {
OutputStream os = new FileOutputStream(tempFile);
// This will copy the file from the two streams
IOUtils.copy(is, os);
// This will close two streams catching exception
IOUtils.closeQuietly(os);
IOUtils.closeQuietly(is);
}
In my app, I'm uploading files to the google drive using GD API. It works fine for small file sizes, but when file size is large (ex: 200MB), it throws java.lang.OutOfMemoryError: exception. I know why it crashes it loads the whole data into the memory, can anyone suggest how can I fix this problem?
This is my code:
OutputStream outputStream = result.getDriveContents().getOutputStream();
FileInputStream fis;
try {
fis = new FileInputStream(file.getPath());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[8192];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
byte[] photoBytes = baos.toByteArray();
outputStream.write(photoBytes);
outputStream.close();
outputStream = null;
fis.close();
fis = null;
} catch (FileNotFoundException e) {
}
This line would allocate 200 MB of RAM and can definitely cause OutOfMemoryError exception:
byte[] photoBytes = baos.toByteArray();
Why are you not writing directly to your outputStream:
while (-1 != (n = fis.read(buf)))
outputStream.write(buf, 0, n);
I've a problem with android and Storage Options (Internal Storage).
I added a SSL certificate in the "raw" directory and i want to write in, into the storage.
In the moment I'm trying it with this code snippet:
InputStream is = SA.getResources().openRawResource(R.raw.myResource);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1)
{
buffer.write(data, 0, nRead);
}
buffer.flush();
FileOutputStream fos = SA.openFileOutput("Resource.crt", 0);
fos.write(buffer.toByteArray());
fos.close();
is.close();
buffer.close();
File f = new File(this.fileList()[0]);
if(f.exists())
{
Log.v("File:", "Found");
}
else
{
Log.v("File:", "Not found");
}
My File isn't found. I don't know why.
I believe fileList is just returning the filename, but you also need the directory try :
File f = new File(context.getFilesDir(), filename);
if(f.exists()){
...
Read here :
http://developer.android.com/training/basics/data-storage/files.html#WriteInternalStorage