incomplete download of file from url in java - using java NIO - java

I used the code provided in accepted solution of below thread to download a 500kb zip file
How to download and save a file from Internet using Java?
public static File downloadFile(String fileURL, String saveDir)
throws IOException {
File downloadFolder = null;
String saveFilePath = null;
URL url = new URL(fileURL);
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
String fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length());
FileOutputStream fos = new FileOutputStream(fileName);
saveFilePath = saveDir + File.separator + fileName;
downloadFolder = new File(saveDir);
downloadFolder.deleteOnExit();
downloadFolder.mkdirs();
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
outputStream.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
outputStream.close();
rbc.close();
return new File(saveFilePath);
}
The code is able to identify the file but the the problem is , the download is incomplete. It always downloads 5KB and stops there after.
I dont want to use apache commons file untils.

The problem was that I didn't set authentication. I set the authentication and it worked. code below
public static File downloadFile(String fileURL, String saveDir)
throws IOException {
File downloadFolder = null;
String saveFilePath = null;
String username = "guest";
String password = "guest";
String usepass = username + ":" + password;
String basicAuth = "Basic "+ javax.xml.bind.DatatypeConverter.printBase64Binary(usepass.getBytes());
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) website.openConnection();
httpConn.setRequestProperty("Authorization", basicAuth);
ReadableByteChannel rbc = Channels.newChannel(httpConn.getInputStream());
String fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,fileURL.length());
FileOutputStream fos = new FileOutputStream(fileName);
saveFilePath = saveDir + File.separator + fileName;
downloadFolder = new File(saveDir);
downloadFolder.deleteOnExit();
downloadFolder.mkdirs();
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
outputStream.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
outputStream.close();
rbc.close();
return new File(saveFilePath);
}

Related

How to generate a dynamic word document from a doc template in java?

I have a word doc template where I have to fill the values dynamically from a Java object. The document template will be in the project classpath, and I will have to fill the values dynamically and save that same file to a shared folder location.
I have used JCIFS to save files to a restricted shared location. The file is created successfully, but the content is not generated correctly. Following is the code,
String user = "username:pwd";
String fileName = outputTemp.docx;
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
System.out.println("Auth =" + auth);
String path = "smb: path" + fileName;
System.out.println("SMB PATH =" + path);
String output = "Downloads\\Projctnam\\src\\main\\resources\\templates\\";
DocxStamper stamper = new DocxStamper(new DocxStamperConfiguration());
InputStream inputStream = new FileInputStream(new File("template.docx"));
OutputStream out = new FileOutputStream(output+fileName);
SmbFile sFile = new SmbFile(path, auth);
SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
sfos.write(out.toString().getBytes());
stamper.stamp(inputStream, variables, out);
sfos.close();
out.close();
This is how the content is added to the file,
The code seems to be fine for me.
So, I found out the way, it's working perfectly fine now
String user = "username:pwd";
String fileName = outputTemp.docx;
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
System.out.println("Auth =" + auth);
String path = "smb: path" + fileName;
System.out.println("SMB PATH =" + path);
String output = "Downloads\\Projctnam\\src\\main\\resources\\templates\\";
DocxStamper stamper = new DocxStamper(new DocxStamperConfiguration());
InputStream inputStream = new FileInputStream(new File("template.docx"));
//OutputStream out = new FileOutputStream(output+fileName);
SmbFile sFile = new SmbFile(path, auth);
SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
//sfos.write(out.toString().getBytes());
stamper.stamp(inputStream, variables, sfos);
sfos.close();
out.close();

Exporting jasper report to excel form jsp results in "file format and extension of .xls" don't match [duplicate]

This question already has answers here:
HTTP response header content disposition for attachments
(5 answers)
How to set 'Content-Disposition' and 'Filename' when using FileSystemResource to force a file download file?
(5 answers)
AngularJS $http-post - convert binary to excel file and download
(9 answers)
How to fix file format and extension don't match?
(4 answers)
Closed 2 years ago.
I am new to iReport. I'm using iReport 3.7.4.
My question is that when I export report to excel calling by jsp, java .file is downloaded and on opening the file it give me the error that "file and extension of .xls" don't match.
The file could be corrupt and mismatched.
Can anyone resolve my issues please thanks in advance.
Here is my export code of java jsp:
Connection con = null;
try {
Dao dao = new Dao();
List parm = new ArrayList();
con = adminSession.con;
String progId = request.getParameter("progId");
String termId = request.getParameter("term");
String testId=" 303";
HashMap map = new HashMap();
map.put("P_TERM_ID", termId);
String rootFolder = null;
String ServletPath = request.getServletPath();
String ServletName = ServletPath.substring(1);
String ServletPathReal = application.getRealPath(ServletPath);
String sep = "";
String ReportPath = getServletConfig().getServletContext().getRealPath("/Images/");
if (ServletPathReal.indexOf("\\") > 0) {
sep = "\\";
} else {
sep = "/";
}
ReportPath = ServletPathReal.substring(0, ServletPathReal.lastIndexOf(sep));
String reportTitle = request.getParameter("title");
if (reportTitle != null && reportTitle.length() > 0) {
reportTitle = reportTitle.toUpperCase();
}
FileInputStream input = null;
JasperPrint jasperPrint = null;
String reportName = "CandidatesUnpaidChallan";
String reportPath = java.io.File.separatorChar + "";
String subPath = getServletConfig().getServletContext().getRealPath("/");
subPath = subPath + reportPath + "reports" + reportPath;
String path = getServletConfig().getServletContext().getRealPath("/reports");
String name = "CandidatesUnpaidChallan.jrxml";
String totalPath = path + File.separator + name;
JasperReport jasperReport = null;
jasperReport = JasperCompileManager.compileReport(totalPath);
jasperPrint = JasperFillManager.fillReport(jasperReport,map, con);
String filename = path + File.separator + "CandidatesUnpaidChallan";
java.util.Date dNow = new java.util.Date();
SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddhhmmss");
String dateAppend = ft.format(dNow);
String excelOutput = filename;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
jasperPrint.setProperty("net.sf.jasperreports.export.xls.exclude.origin.keep.first.band.1", "pageHeader");
jasperPrint.setProperty("net.sf.jasperreports.export.xls.exclude.origin.keep.first.band.2", "columnHeader");
jasperPrint.setProperty("net.sf.jasperreports.export.xls.exclude.origin.band.2", "pageFooter");
JExcelApiExporter jExcelApiExporter = new JExcelApiExporter();
jExcelApiExporter.setParameter(JExcelApiExporterParameter.JASPER_PRINT, jasperPrint);
jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
jExcelApiExporter.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM, outputStream);
jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_IGNORE_CELL_BORDER,Boolean.TRUE);
jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);
jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
jExcelApiExporter.setParameter(JExcelApiExporterParameter.OFFSET_X,0);
jExcelApiExporter.setParameter(JExcelApiExporterParameter.OFFSET_Y,0 );
jExcelApiExporter.setParameter(JExcelApiExporterParameter.OUTPUT_FILE_NAME, excelOutput);
jExcelApiExporter.exportReport();
File file = new File(filename+".xlsx");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Cache-Control", "max-age=30");
response.setHeader("Content-Disposition", "attachment;filename="+reportName+".xlsx");
response.setHeader("Content-disposition", "inline; filename=\"" + reportName + "_" + dateAppend + ".xlsx\"");
input = new FileInputStream(file);
int readBytes = 0;
BufferedInputStream buf = new BufferedInputStream(input);
stream = response.getOutputStream();
while ((readBytes = buf.read()) != -1) {
stream.write(readBytes);
}
stream.flush();
input.close();
stream.close();
} catch (Exception oops) {
System.out.println(oops);
con = null;
throw new Exception("Error occured while showing Evaluations~" + oops.toString());
} finally {
con = null;
}

Create Tar archive from directory on S3 using AWS Lambda

I need to extract a bunch of zip files stored on s3 and add them to a tar archive and store that archive on s3. it is likely that that the sum of the zip files will greater than the 512mb local storage allowed from lambda functions. I have a partial souldtion that gets the objects from s3 extracts them and puts them in a s3 object without using the lambda local storage.
Extract object Thread
public class ExtractObject implements Runnable{
private String objectName;
private String uuid;
private final byte[] buffer = new byte[1024];
public ExtractAdvert(String name, String uuid) {
this.objectName= name;
this.uuid= uuid;
}
#Override
public void run() {
final String srcBucket = "my-bucket-name";
final AmazonS3 s3Client = new AmazonS3Client();
try {
S3Object s3Object = s3Client.getObject(new GetObjectRequest(srcBucket, objectName));
ZipInputStream zis = new ZipInputStream(s3Object.getObjectContent());
ZipEntry entry = zis.getNextEntry();
while(entry != null) {
String fileName = entry.getName();
String mimeType = FileMimeType.fromExtension(FilenameUtils.getExtension(fileName)).mimeType();
System.out.println("Extracting " + fileName + ", compressed: " + entry.getCompressedSize() + " bytes, extracted: " + entry.getSize() + " bytes, mimetype: " + mimeType);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int len;
while ((len = zis.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(outputStream.size());
meta.setContentType(mimeType);
System.out.println("##### " + srcBucket + ", " + FilenameUtils.getFullPath(objectName) + "tmp" + File.separator + uuid + File.separator + fileName);
// Add this to tar archive instead of putting back to s3
s3Client.putObject(srcBucket, FilenameUtils.getFullPath(objectName) + "tmp" + File.separator + uuid + File.separator + fileName, is, meta);
is.close();
outputStream.close();
entry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
}
this runs for each object that needs to be extracted and saves them in a s3 object in the structure needed for the tar file.
I think what i need is instead of putting the object back to s3 is to keep it in memory and add it to a tar archive. and upload that but after a lot of looking around and trial and error i have not created a successful tar file.
The main issue is i can't use the tmp directory in lambda.
Edit
should i be creating the tar file as i go instead of putting objects to s3? (see comment // Add this to tar archive instead of putting back to s3)
if so how do i create a tar stream without a storing it locally?
EDIT 2: Attempt at taring the files
ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(bucketName);
ListObjectsV2Result result;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TarArchiveOutputStream tarOut = new TarArchiveOutputStream(baos);
do {
result = s3Client.listObjectsV2(req);
for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
if(objectSummary.getKey().startsWith("tmp/") ) {
System.out.printf(" - %s (size: %d)\n", objectSummary.getKey(), objectSummary.getSize());
S3Object s3Object = s3Client.getObject(new GetObjectRequest(bucketName, objectSummary.getKey()));
InputStream is = s3Object.getObjectContent();
System.out.println("Pre Create entry");
TarArchiveEntry archiveEntry = new TarArchiveEntry(IOUtils.toByteArray(is));
// Getting following exception above
// IllegalArgumentException: Invalid byte 111 at offset 7 in ' positio' len=8
System.out.println("Pre put entry");
tarOut.putArchiveEntry(archiveEntry);
System.out.println("Post put entry");
}
}
String token = result.getNextContinuationToken();
System.out.println("Next Continuation Token: " + token);
req.setContinuationToken(token);
} while (result.isTruncated());
ObjectMetadata metadata = new ObjectMetadata();
InputStream is = new ByteArrayInputStream(baos.toByteArray());
s3Client.putObject(new PutObjectRequest(bucketName, bucketFolder + "tar-file", is, metadata));
I have found a solution to this and it very similar to my attempt in Edit 2 above.
private final String bucketName = "bucket-name";
private final String bucketFolder = "tmp/";
private final String tarKey = "tar-dir/tared-file.tar";
private void createTar() throws IOException, ArchiveException {
ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(bucketName);
ListObjectsV2Result result;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TarArchiveOutputStream tarOut = new TarArchiveOutputStream(baos);
do {
result = s3Client.listObjectsV2(req);
for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
if (objectSummary.getKey().startsWith(bucketFolder)) {
S3Object s3Object = s3Client.getObject(new GetObjectRequest(bucketName, objectSummary.getKey()));
InputStream is = s3Object.getObjectContent();
String s3Key = objectSummary.getKey();
String tarPath = s3Key.substring(s3Key.indexOf('/') + 1, s3Key.length());
s3Key.lastIndexOf('.'));
byte[] ba = IOUtils.toByteArray(is);
TarArchiveEntry archiveEntry = new TarArchiveEntry(tarPath);
archiveEntry.setSize(ba.length);
tarOut.putArchiveEntry(archiveEntry);
tarOut.write(ba);
tarOut.closeArchiveEntry();
}
}
String token = result.getNextContinuationToken();
System.out.println("Next Continuation Token: " + token);
req.setContinuationToken(token);
} while (result.isTruncated());
ObjectMetadata metadata = new ObjectMetadata();
InputStream is = baos.toInputStream();
metadata.setContentLength(baos.size());
s3Client.putObject(new PutObjectRequest(bucketName, tarKey, is, metadata));
}

How to download/export .txt file in java?

I formed an url in the controller.When I hit that url i need to export a .txt file.As I am new to this concept , I have a doubts ,
1) Do we need to import any jar file to export .txt file as like we add jars for pdf and xls ?
I have tried like below..But i dont get any result by it.I didn't add any jar file ..
FileWriter writer = new FileWriter("MyFile.txt", true);
writer.write("Hello World");
writer.write("\r\n"); // write new line
writer.write("Good Bye!");
writer.close();
In a couple of projects I've used this utility class from codejava.net
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* A utility that downloads a file from a URL.
* #author www.codejava.net
*
*/
public class HttpDownloadUtility {
private static final int BUFFER_SIZE = 4096;
/**
* Downloads a file from a URL
* #param fileURL HTTP URL of the file to be downloaded
* #param saveDir path of the directory to save the file
* #throws IOException
*/
public static void downloadFile(String fileURL, String saveDir)
throws IOException {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();
if (disposition != null) {
// extracts file name from header field
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10,
disposition.length() - 1);
}
} else {
// extracts file name from URL
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
fileURL.length());
}
System.out.println("Content-Type = " + contentType);
System.out.println("Content-Disposition = " + disposition);
System.out.println("Content-Length = " + contentLength);
System.out.println("fileName = " + fileName);
// opens input stream from the HTTP connection
InputStream inputStream = httpConn.getInputStream();
String saveFilePath = saveDir + File.separator + fileName;
// opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
System.out.println("File downloaded");
} else {
System.out.println("No file to download. Server replied HTTP code: " + responseCode);
}
httpConn.disconnect();
}
}
The code i have written is just a three lines to download the .txt file.
Thank you all For the help.
I am just posting my answer because just to download a empty file who need for the beginners.
Adding HttpServletResponse servletResponse dependency,
OutputStream out = servletResponse.getOutputStream();
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"Report"+".txt\";");
servletResponse.setHeader(headerKey, headerValue);
// obtains response's output stream
OutputStream outStream = servletResponse.getOutputStream();
outStream.close();

How can I delete picture programmatically in Android?

I wrote some code that lets me save pictures in my data/data in Android internal storage. Now I would like to know if there is a way to delete those pictures from internal storage.
Here is what I have for saving:
public boolean saveImg( String showId ) {
try {
URL url = new URL(getImgUrl( showId ));
File file = new File(showId + ".jpg");
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
//Define InputStreams to read from the URLConnection.
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
//Read bytes to the Buffer until there is nothing more to read(-1).
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
//Convert the Bytes read to a String.
FileOutputStream fos = new FileOutputStream(PATH+file);
fos.write(baf.toByteArray());
fos.close();
return true;
} catch (IOException e) {
return false;
}
}
I tried this but it doesn't delete from data/data. Any suggestions as to what I'm doing wrong?
public void DeleteImg(String showId) {
File file = new File( PATH + showId +".jpg" );
file.delete();
}
Try this:
File file = new File(selectedFilePath);
boolean deleted = file.delete();

Categories

Resources