I have a piece of code that read my inbox email. the code identify 2 kind of attachments:
1) The attached files, which are downloaded and saved to a database. This works just fine.
2) The inline attachment (I'm using an image as the attachment in my tests). The code detects this kind of attachment, but when I save them to disk the file seems to be corrupted. I checked the file properties generated and noticed that it had no basic info (pixels, height, width) with it. I think the file is not saved properly when downloaded to disk (I have tried PNG and JPG). I think the file needs to be saved with a kind of mimetype properties so i can open it properly. Any tips please? Thanks in advance.!
Here is a snip of my code:
public void procesMultiPart(Multipart content) throws SQLException {
try {
for (int i = 0; i < content.getCount(); i++) {
BodyPart bodyPart = content.getBodyPart(i);
Object o;
String downloadDirectory = "D:/Attachment/";
o = bodyPart.getContent();
if (o instanceof String) {
System.out.println("procesMultiPart es plainText");
} else if (null != bodyPart.getDisposition() && bodyPart.getDisposition().equalsIgnoreCase(Part.ATTACHMENT)) {
System.out.println("IS ATTACHMENT...");
// i save the attachment to database and is OK..
} else if (bodyPart.getDisposition() == null){
System.out.println("IS AN INLINE ATTACHMENT...");
String fileNameinline = "inline" + i + ".png";
InputStream inStream = bodyPart.getInputStream();
FileOutputStream outStream = new FileOutputStream(new File(downloadDirectory + fileNameinline), true);
byte[] tempBuffer = new byte[4096];// 4 KB
int numRead;
while ((numRead = inStream.read(tempBuffer)) != -1) {
outStream.write(tempBuffer);
}
inStream.close();
outStream.close();
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
The file is corrupted because you are writing it to the disc incorrectly:
outStream.write(tempBuffer);
You should only write as many bytes as you read:
outStream.write(tempBuffer, 0, numRead);
Thanks to everyone for help and tips. I solved the problem. This is how i did it: I notice that when read the email body (where supposes there was just the pasted image) , there was more that just the image, there was a kind of HTML too, so in other words, the body part has multiparts (2 parts in my case), so i have to process each part until find the image, so i can save it.
hope this help someone else.
regards.
Related
This question already has answers here:
How do I create a file and write to it?
(35 answers)
Closed 1 year ago.
I'm working on a new function on a system, and at the moment I need to pick a file, and save on a folder in the user C:. The process happens as it follows, the user uploads a file into the system, that file can be anything, text, image, except videos, and now it is saved in the system database, but my boss wants to change that process so I need to save on a specific folder on the user C:, I already created the specific folder, but I don't know how to save the file in that created folder.
So the code for uploading a file as it follows:
public void uploadArquivo(FileUploadEvent event) {
byte[] bytes = null;
try {
File targetFolder = new File(System.getProperty("java.io.tmpdir"));
if (!targetFolder.exists()) {
if (targetFolder.mkdirs()) {
FacesMessageUtil.novaMensagem("Atenção", "Não foi possível criar pasta temporária!");
return;
}
}
targetFolder.mkdirs();
OutputStream out;
try (InputStream inputStream = event.getFile().getInputstream()) {
out = new FileOutputStream(new File(targetFolder, event.getFile().getFileName()));
int read;
bytes = new byte[10485760];
while ((read = inputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
inputStream.close();
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace(System.out);
}
AnexoEmpreendimento anexo = new AnexoEmpreendimento();
anexo.setNomeArquivo(event.getFile().getFileName());
anexo.setTamanhoArquivo(event.getFile().getSize());
anexo.setArquivo(bytes);
anexos.add(anexo);
}
Well, it depends on what you actually 'have'. A web submit form? Which web framework do you have then? A BLOB in a db? Which DB engine do you have and which DB framework are you using in java to interact with it, etcetera.
Most libraries will let you obtain either an InputStream or a byte[] (if they offer both, you want the InputStream).
You can write an inputstream to a file, or a byte[] to a file, as follows:
import java.nio.file.*;
public class Example {
static final Path MY_DIR = Paths.get("C:/path/to/your/dir");
void writeByteArr(byte[] data) throws IOException {
Path toWrite = MY_DIR.resolve("filename.dat");
Files.write(toWrite, data);
}
void writeInputStream(InputStream data) throws IOException {
Path toWrite = MY_DIR.resolve("filename.dat");
Files.copy(data, toWrite);
}
}
In the unlikely case the data you 'get' is neither in byte[] nor InputStream form you're going to have to elaborate quite a bit on how the data gets to your code.
I have a java application where users can upload and download files. Recently, we found out that whenever users click on a link on IE11 to download .docx or a .xlsx file, it downloads a .doc or .xls file. In the process, it warns the users that the file format and extension do not match and the users should only open the file if they trust its source. There is no such issue on Microsoft Edge or other browsers.
Is there some setting that can be done in IE11 or can some coding (specific to IE11) be done so that so that it downloads .xlsx and .docx files as they are and doesn't give annoying warning messages to users?
try {
byte[] fileContent = getFileContent(id, fName);
if (fileContent != null) {
OutputStream out = null;
try {
System.out.println("content type: "+getContentType(fName)); //prints application/vnd.ms-excel
res.reset();
out = res.getOutputStream();
res.setContentType(getContentType(fName));
res.setHeader("Content-Disposition", "inline; filename=" + fName + "; size=" + String.valueOf(fileContent.length));
res.setContentLength(fileContent.length);
out.write(fileContent);
setDestination(req, RESPONSE_NO_REDIRECT);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
flushCloseOutputStream(out);
}
} else {
setDestination(req, "/404.jsp");
}
} catch (Exception ex) {
ex.printStackTrace();
}
public byte[] getFileContent(int id, String fileName) {
byte[] bytes = null;
Transaction tx = null;
Session s = null;
try {
GenericDAO dao = HibernateDAOFactory.getInstance().getDAO(GenericClassDAO.class, Files.class);
s = SessionAndTransactionManagementService.createNewSession(dao);
tx = SessionAndTransactionManagementService.startNewTransaction(s);
Criteria cr = s.createCriteria(Files.class)
.add(Restrictions.eq("id", id))
.add(Restrictions.eq("fileName", fileName))
.setProjection(Projections.property("fileContent"));
bytes = (byte[]) cr.uniqueResult();
SessionAndTransactionManagementService.commitTransaction(s);
} catch (Exception e) {
HibernateUtil.rollback(tx);
}finally{
HibernateUtil.cleanupResources(s);
}
return bytes;
}
IE11 was setting content type for all excel files as 'application/vnd.ms-excel' (don't know why). That makes it show warnings and download xlsx files as xls.
I changed my code to set contentType manually to 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' when the filename contains .xlsx and this solved my problem.
I have the following problem trying to create a PDF from an array of byte recovered from a Web Services output object.
So I have the following situation:
OutFat outFat = callServizioContabRighe(dataEM, esercizio, importo, bolla, numDoc, oda, pIva, contratto);
byte[] pdfByteArray = outFat.getPDF();
ByteArrayOutputStream osPdf = new ByteArrayOutputStream();
if(pdfByteArray != null) {
InputStream is = new ByteArrayInputStream(pdfByteArray);
sun.misc.BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] byteArray64DecodePdf = null;
try {
byteArray64DecodePdf = base64Decoder.decodeBuffer(is);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
osPdf.write(byteArray64DecodePdf);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
if(osPdf != null && pdfByteArray != null){
req.getSession().setAttribute("PdfEM", osPdf); // Usa il PDF recuperato dal WS
}
else {
req.getSession().setAttribute("PdfEM", createPdfEM.createPdf()); // Crea il PDF
}
if(osPdf != null && pdfByteArray != null){
this.baos = osPdf;
}
else {
this.baos = createPdfEM.createPdf(); // CREA IL PDF
}
Ok so I have the pdfByteArray that is the byte array retrieved from the outFat object (the webservice output object) and that represent my PDF.
Now I have to use it to build a PDF and show it into a view.
So to create the PDF I do the following steps:
I create an InputStream object using the pdfByteArray retrieved by the WS.
I decode it (it is in base64 so I need to decode it).
I write the decoded byteArray64DecodePdf into the osPdf outpustream object.
Then, if the previous steps are successful, I put it into my view (otherwise I create my PDF in an alternative way but this is not related to this question).
The problem is that in my view I can't see the content of my PDF but I only see a grey square rather than it.
What could be the problem?
What can I do to save a local copy of my PDF to try to see it on my local system instead into the browser? So I can understand if the problem is in the conversion or elsewhere
Tnx
I want to create zip file of files which are present at one ftp location and Copy this zip file to other ftp location without saving locally.
I am able to handle this for small size of files.It works well for small size files 1 mb etc
But if file size is big like 100 MB, 200 MB , 300 MB then its giving error as,
java.io.FileNotFoundException: STOR myfile.zip : 550 The process cannot access the
file because it is being used by another process.
at sun.net.ftp.FtpClient.readReply(FtpClient.java:251)
at sun.net.ftp.FtpClient.issueCommand(FtpClient.java:208)
at sun.net.ftp.FtpClient.openDataConnection(FtpClient.java:398)
at sun.net.ftp.FtpClient.put(FtpClient.java:609)
My code is
URLConnection urlConnection=null;
ZipOutputStream zipOutputStream=null;
InputStream inputStream = null;
byte[] buf;
int ByteRead,ByteWritten=0;
***Destination where file will be zipped***
URL url = new URL("ftp://" + ftpuser+ ":" + ftppass + "#"+ ftppass + "/" +
fileNameToStore + ";type=i");
urlConnection=url.openConnection();
OutputStream outputStream = urlConnection.getOutputStream();
zipOutputStream = new ZipOutputStream(outputStream);
buf = new byte[size];
for (int i=0; i<li.size(); i++)
{
try
{
***Souce from where file will be read***
URL u= new URL((String)li.get(i)); // this li has values http://xyz.com/folder
/myPDF.pdf
URLConnection uCon = u.openConnection();
inputStream = uCon.getInputStream();
zipOutputStream.putNextEntry(new ZipEntry((String)li.get(i).substring((int)li.get(i).lastIndexOf("/")+1).trim()));
while ((ByteRead = inputStream .read(buf)) != -1)
{
zipOutputStream.write(buf, 0, ByteRead);
ByteWritten += ByteRead;
}
zipOutputStream.closeEntry();
}
catch(Exception e)
{
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream .close();
}
catch (Exception e) {
e.printStackTrace();
}
}
if (zipOutputStream != null) {
try {
zipOutputStream.close();
} catch (Exception e){
e.printStackTrace();
}
}
Can anybody let me know how I can avoid this error and handle large files
This is unrelated to file sizes; as the error says, you can't replace the file because some other process is currently locking it.
The reason why you see it more often with large files is because these take longer to transfer hence the chance of concurrent accesses is higher.
So the only solution is to make sure that no one uses the file when you try to transfer it. Good luck with that.
Possible other solutions:
Don't use Windows on the server.
Transfer the file under a temporary name and rename it when it's complete. That way, other processes won't see incomplete files. Always a good thing.
Use rsync instead of inventing the wheel again.
Back in the day, before we had network security, there were FTP servers that allowed 3rd party transfers. You could use site specific commands and send a file to another FTP server directly. Those days are long gone. Sigh.
Ok, maybe not long gone. Some FTP servers support the proxy command. There is a discussion here: http://www.math.iitb.ac.in/resources/manuals/Unix_Unleashed/Vol_1/ch27.htm
I'm currently building an android application that displays a set of pdf files in a ListView. Instead of just displaying the filename I want to grab the Title from the metadata of the pdf and display that in the list, if the file doesnt have a Title set then just use the filename. I'm using iText atm, here is what I have:
File[] filteredFiles = root.listFiles(filter);
for (int i=0;i<filteredFiles.length;i++) {
try {
File f = filteredFiles[i];
PdfReader reader = new PdfReader(f.getAbsolutePath());
String title = reader.getInfo().get("Title");
reader.close();
//Do other stuff here...
} catch (Exception e) {
e.printStackTrace();
}
}
This works fine, its gets the data I want, but its slowww. Also, sometimes I get memory crashes if the file is over 2MB. Is there a better way of doing this? Maybe a way of getting the metadata without having to actually open the pdf file?
Any help is much appreciated, Thanks.
You can try fast PDFParse library. It optimized for performance & small memory consumption.
File[] filteredFiles = root.listFiles(filter);
for (int i=0;i<filteredFiles.length;i++) {
try {
File f = filteredFiles[i];
PDFDocument reader = new PDFDocument(f.getAbsolutePath());
String title = reader.getDocumentInfo().getTitle();
reader.close();
//Do other stuff here...
} catch (Exception e) {
e.printStackTrace();
}
}