korrupt rar fil
Af en eller anden grund er de rar filer jeg henter fra FTP-serveren korrupte. jeg har mistanke om at det er i det her stykke kode fejlen ligger. Når jeg henter dem manuelt fra serveren er der ikke noget galt med dem.import java.io.File;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class FtpGet implements Runnable{
static boolean runFG = false;
static boolean getFG = false;
static boolean ffgDone = false;
static boolean err = false;
Thread FG = new Thread(this);
static String[] files;
String server;
String user;
String pass;
String downDir;
String localDir;
String fileType;
boolean delFiles;
boolean switchBT = false;
OutputStream os;
FTPClient client = new FTPClient();
FtpGet(){
FG.start();
}
public void startThread(){
runFG = true;
}
public void getFileThread(){
getFG = true;
}
public void getJobFileNames(){
ffgDone = false;
try{
getConfig();
client.connect(server);
PLZIP.sysLog.addTxt("Forbinder til "+server+" med "+user,PLZIP.sysLog.bluTxt);
if(client.login(user,pass)){
if(client.changeWorkingDirectory(downDir)&&downDir!=null){
PLZIP.sysLog.addTxt("Tjekker filstatus i "+downDir,PLZIP.sysLog.bluTxt);
FTPFile[] getFiles = client.listFiles("*."+fileType);
client.disconnect();
files = new String[getFiles.length];
for(int i=0;i<getFiles.length;i++){
if(getFiles[i].getName().compareTo("..")>0){
files[i] = getFiles[i].getName();
}
}
ffgDone = true;
}
}else{
PLZIP.sysLog.addTxt("Kan ikke få adgang, tjek navn og kode",PLZIP.sysLog.redTxt);
}
}catch (SocketException e) {
PLZIP.sysLog.addTxt("Kan ikke få forbindelse til "+server,PLZIP.sysLog.redTxt);
e.printStackTrace();
} catch (IOException e) {
PLZIP.sysLog.addTxt("IO fejl i forbindelsen til "+server,PLZIP.sysLog.redTxt);
e.printStackTrace();
}
}
public void getConfig(){
try{
File configFile = new File("data\\PLZIPconfig.cfg");
FileReader configReader = new FileReader(configFile);
char[] readBuffer = new char[(int)configFile.length()];
configReader.read(readBuffer);
String configString = new String(readBuffer);
String[] configArr = configString.split(",");
try{
server = configArr[0];
}catch (ArrayIndexOutOfBoundsException e){}
try{
user = configArr[1];
}catch (ArrayIndexOutOfBoundsException e){}
try{
pass = configArr[2];
}catch (ArrayIndexOutOfBoundsException e){}
try{
downDir = configArr[4];
}catch (ArrayIndexOutOfBoundsException e){}
try{
localDir = configArr[5];
}catch (ArrayIndexOutOfBoundsException e){}
try{
fileType = configArr[6];
}catch (ArrayIndexOutOfBoundsException e){}
try{
if(Integer.parseInt(configArr[8])==1){
delFiles = true;
}else{
delFiles = false;
}
}catch (ArrayIndexOutOfBoundsException e){}
}catch(IOException e){}
}
public void run(){
while(true){
if(runFG){
if(PLZIP.btProceed.isEnabled()){
PLZIP.btProceed.setEnabled(false);
switchBT = true;
}
try {
//Setup progressBar
PLZIP.pgBar.setMinimum(0);
PLZIP.pgBar.setMaximum(files.length);
PLZIP.pgBar.setValue(0);
getConfig();
client.connect(server);
client.setFileType(FTPClient.BINARY_FILE_TYPE);
err = false;
if(client.login(user,pass)){
PLZIP.sysLog.addTxt("Forbundet til ftp:\\\\"+server,PLZIP.sysLog.bluTxt);
if(client.changeWorkingDirectory(downDir)&&downDir!=null){
PLZIP.sysLog.addTxt("Skiftet til "+downDir+" mappen",PLZIP.sysLog.bluTxt);
FTPFile[] getFiles = client.listFiles("*."+fileType);
for(int i = 0;i<getFiles.length; i++){
PLZIP.sysLog.addTxt("Henter "+getFiles[i].getName(),PLZIP.sysLog.bluTxt);
String destDir = localDir+getFiles[i].getName().substring(0,2)+"000-"+getFiles[i].getName().substring(0,2)+"999\\"+getFiles[i].getName().substring(0,3)+"00-"+getFiles[i].getName().substring(0,3)+"99\\"+getFiles[i].getName().substring(0,5)+"\\org\\";
try{
os = new BufferedOutputStream(new FileOutputStream(new File(destDir+getFiles[i].getName())));
}catch(FileNotFoundException ev){
File tmpDirs = new File(destDir);
PLZIP.sysLog.addTxt("Opretter mappe til "+getFiles[i].getName(),PLZIP.sysLog.greTxt);
tmpDirs.mkdirs();
os = new BufferedOutputStream(new FileOutputStream(new File(destDir+getFiles[i].getName())));
}
if(client.retrieveFile(getFiles[i].getName(),os)){
PLZIP.sysLog.addTxt(getFiles[i].getName()+" hentet",PLZIP.sysLog.greTxt);
if(delFiles){
PLZIP.sysLog.addTxt("Sletter "+getFiles[i].getName()+" fra FTP",PLZIP.sysLog.bluTxt);
client.deleteFile(getFiles[i].getName());
}
}else{
PLZIP.sysLog.addTxt("Kunne ikke hente "+getFiles[i].getName(),PLZIP.sysLog.redTxt);
}
os.close();
os = null;
//UnZip UZ = new UnZip(destDir+getFiles[i].getName(),destDir);
PLZIP.pgBar.setValue((PLZIP.pgBar.getValue()+1));
}
}else{
err = true;
PLZIP.sysLog.addTxt("Kunne ikke skifte til "+downDir+" mappen",PLZIP.sysLog.redTxt);
}
}else{
err = true;
PLZIP.sysLog.addTxt("Kan ikke få adgang, tjek navn og kode",PLZIP.sysLog.redTxt);
}
if(!err){
PLZIP.sysLog.addTxt("Alle filer hentet",PLZIP.sysLog.greTxt);
}
client.disconnect();
PLZIP.sysLog.addTxt("Forbindelse lukket til "+server,PLZIP.sysLog.bluTxt);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(switchBT){
PLZIP.btProceed.setEnabled(true);
switchBT = false;
}
runFG = false;
}else if(getFG){
getJobFileNames();
try{
if(files.length>0){
PLZIP.sysLog.addTxt("Der er "+files.length+" filer klar",PLZIP.sysLog.greTxt);
for(int i=0;i<files.length;i++){
PLZIP.sysLog.addTxt(files[i],PLZIP.sysLog.bluTxt);
}
PLZIP.btGet.setEnabled(true);
}else{
PLZIP.sysLog.addTxt("Der er ingen filer klar",PLZIP.sysLog.yelTxt);
}
}catch(NullPointerException e){}
getFG = false;
}else{
try{
Thread.sleep(200);
}catch(InterruptedException e){}
}
}
}
}