Hi,
I'm trying export all attachments from Oracle Sales Cloud, with a java aplication.
I already created a java code to download the file, but the file come with error.
The files have many extensions (txt, pdf, xls), and I have to download all of them.
The pdf file I cannot open because occurs a error that say the file is corrupted, and the txt file come with the data of HTML.
I see this HTML is a Login page.
So, I understand that I have to create one more authentication for this (Because I only can downlod the files if I login in).
But I don't have idea of how I create that.
I will share my code here, and tell me if I'm doing something wrong.
package br.osc.oportunidade;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import javax.xml.bind.JAXBElement;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.soap.AddressingFeature;
import br.org.oportunidade.CommonAttachments;
import br.org.oportunidade.GetAttachmentContent;
import br.org.oportunidade.GetAttachmentsListWithoutContent;
import br.org.oportunidade.GetAttachmentsListWithoutContentResponse;
import br.org.oportunidade.ObjectFactory;
import br.org.oportunidade.OpportunityService;
import br.org.oportunidade.OpportunityService_Service;
import br.org.oportunidade.ServiceException;
public class PegarAnexos {
protected static OpportunityService opportunityService;
protected static OpportunityService_Service opportunityService_Service;
public void getAnexo(String userName, String password) throws ServiceException, MalformedURLException {
opportunityService_Service = new OpportunityService_Service();
opportunityService = opportunityService_Service.getOpportunityServiceSoapHttpPort(new AddressingFeature(false));
Map<String, Object> context = ((BindingProvider) opportunityService).getRequestContext();
context.put(BindingProvider.USERNAME_PROPERTY, userName);
context.put(BindingProvider.PASSWORD_PROPERTY, password);
ObjectFactory objectFactory = new ObjectFactory();
CommonAttachments commonAttachments = new CommonAttachments();
commonAttachments.setEntityName(objectFactory.createCommonAttachmentsEntityName("MOO_OPTY"));
commonAttachments.setPk1Value(objectFactory.createCommonAttachmentsPk1Value("300000010364510"));
commonAttachments.setDownloadStatus(objectFactory.createCommonAttachmentsDownloadStatus("1"));
commonAttachments.setDatatypeCode(objectFactory.createCommonAttachmentsDatatypeCode("FILE"));
GetAttachmentsListWithoutContent getAttachmentsListWithoutContent = new GetAttachmentsListWithoutContent();
getAttachmentsListWithoutContent.getGetParameters().add(commonAttachments);
List<CommonAttachments> commonAttachments2 = opportunityService
.getAttachmentsListWithoutContent(getAttachmentsListWithoutContent.getGetParameters());
for (CommonAttachments commonAttachments3 : commonAttachments2) {
// System.out.println(commonAttachments3.getTitle().getValue());
// System.out.println(commonAttachments3.getTableName().getValue());
// System.out.println(commonAttachments3.getFileURL().getValue());
try {
// cria URL
URL url1 = new URL(
"https://cbuj.crm.us2.oraclecloud.com/sales" + commonAttachments3.getFileURL().getValue());
// abre uma conexao na url criada àcima
URLConnection con = url1.openConnection();
// tenta conectar.
con.connect();
// arquivo de saida
FileOutputStream fileOut = new FileOutputStream(
"C:/Users/myUser/Desktop/Test/" + commonAttachments3.getTitle().getValue());
int c = 0;
do {
// le o byte
c = con.getInputStream().read();
// escreve o byte no arquivo saida
fileOut.write(c);
} while (c != -1);
// fecha o arquivo de saida
fileOut.close();
System.out.println("Arquivo baixado com sucesso");
System.out.println(url1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) throws ServiceException, MalformedURLException {
PegarAnexos busca = new PegarAnexos();
// pass user and password.
busca.getAnexo("test@test.com.br", "ps12345");
}
}
I attached the download file
Regards
Rafael