I have Spring Boot REST Application that runs on Azure on app service api-app.
I need to read access database files from this path on azure "D:\home\site\wwwroot\database>".
When I read a file from that location I need to delete them.
This is my code for read and delete file from azure, but when I go to KUDU Debug Console, file was not deleted.
public List<AccessFeedDTO> download(Long fileId) throws URISyntaxException, StorageException, IOException, SQLException{
if(fileId!=null){
File file = null;
try {
ImportFileFeedDTO importFileFeedDTO= importFileFeedService.getFeedFile(fileId);
storageAccount = CloudStorageAccount.parse(storageConnectionString);
CloudFileClient fileClient = storageAccount.createCloudFileClient();
CloudFileShare share = fileClient.getShareReference("spartanv3files");
CloudFileDirectory rootDir = share.getRootDirectoryReference();
CloudFile cloudFile = rootDir.getFileReference(importFileFeedDTO.getFile_uuid());
System.out.println(cloudFile.getName());
System.out.println(cloudFile.downloadText());
file = new File ("D:/home/database/"+importFileFeedDTO.getFile_uuid());
FileOutputStream fileOutputStream = new FileOutputStream(file);
cloudFile.download(fileOutputStream);
fileOutputStream.close();
System.gc();
List<AccessFeedDTO> accessFeedsDTOlist= accessFeedMapper.accessFeedEntitytolistofAccessFeedDTO(accessFeedRepository.getAllAccessFeeds(importFileFeedDTO.getFile_uuid()));
file.delete();
return accessFeedsDTOlist;
} catch (InvalidKeyException invalidKey) {
throw new RuntimeException(invalidKey.getMessage());
}finally{
file.delete();
}
}else{
throw new RuntimeException();
}
}
And this is the way how I set up a connection to that file (access database) on azure. I used UCanAccess to connect to temporary Access files for reading data.
public Connection getAccessFeedConnection(String fileName){
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
connection = DriverManager.getConnection("jdbc:ucanaccess://D:/home/database/"+fileName+";immediatelyReleaseResources=true");
return connection;
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
return connection;
}
}
And here is how I use connection to pic up parameters from access database
public List<AccessFeedEntity> getAllAccessFeeds(String dataBaseName) throws SQLException{
Connection connection = accessConfig.getAccessFeedConnection(dataBaseName);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM TBLFEEDS");
try {
List<AccessFeedEntity> listofaccessFeedEntity = new ArrayList<AccessFeedEntity>();
while(resultSet.next()){
AccessFeedEntity accessFeedEntity = new AccessFeedEntity();
accessFeedEntity.setFeedid(resultSet.getLong("ID"));
accessFeedEntity.setFeedname(resultSet.getString("NAME"));
listofaccessFeedEntity.add(accessFeedEntity);
}
connection.close();
statement.close();
resultSet.close();
return listofaccessFeedEntity;
} catch (SQLException e) {
connection.close();
statement.close();
resultSet.close();
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally{
try {
connection.close();
statement.close();
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
When I try to delete that file on Kudu Debug Console I get the following message "The process cannot access the file because it is being used by another process.
"
When I try this on localhost file is deleted. But on Azure doesn't work.
Where I went wrong and where I did not close the stream?
Or can someone help me to delete this file on other way?
Try stopping the app and deleting it from Kudu while the app is off.
Related
how can I fetch movie.list.gz folder from the link
1: ftp://ftp.funet.fi/pub/mirrors/ftp.imdb.com/pub/, I have used a code to access the FTP server for fetching data but it asks for port no. which I don't know. Can anyone please tell how to fetch data from the above link in java. I am developing a Web Application which will display all the movie names present in IMDB database.
CODE:
String server = "ftp://ftp.funet.fi/pub/mirrors/ftp.imdb.com";
int port = 21;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// APPROACH #1: using retrieveFile(String, OutputStream)
String remoteFile1 = "/pub/movie.list.gz";
File downloadFile1 = new File("F:/softwares/mov.list.gz");
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
if (success) {
System.out.println("File #1 has been downloaded successfully.");
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Good day. I have imported org.apache.commons.net.ftp.FTP to my project and I faced some issues when upload the zip file to FTP.
When it uploads small file(<100MB), it works fine. But when it comes to larger file (>500MB), it will stops there, without returning and execute the remaining operation, even it the file is transferred to the server.
And how the function / FTP acknowledge when the transfer is done? Is there anyway to check it?
public class zipFTP
{
public boolean uploadFile(String ht, String usr, String ps, String fpath, String uploadloc)
{
FTPClient client = new FTPClient();
FileInputStream fis = null;
writeLog nLog = new writeLog();
String channel = "FTP";
boolean completed = false;
//client.setBufferSize(1048576);
client.setControlKeepAliveReplyTimeout(300);
//System.out.println(client.getBufferSize());
try
{
nLog.writeToLog(dumptoFTP.filename, channel, "Uploading...");
client.connect(ht);
client.login(usr, ps);
client.enterLocalPassiveMode();
client.setFileType(FTP.BINARY_FILE_TYPE);
fis = new FileInputStream(fpath);
completed = client.storeFile(uploadloc, fis);
//client.completePendingCommand(); //to complete the transaction entirely
if(completed)
{
if (dumptoFTP.isSysLog)
System.out.println("File uploaded");
nLog.writeToLog(dumptoFTP.filename, channel, "File uploaded");
}
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
if (dumptoFTP.isSysLog)
System.out.println("Upload failed");
nLog.writeToLog(dumptoFTP.filename, channel, "Upload failed");
nLog.writeToLog(dumptoFTP.filename, channel, e.toString());
}
finally
{
try
{
if(client.isConnected())
{
client.logout();
client.disconnect();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
return completed;
}
}
Ended up, setBufferSize() does the trick
I want to write a code which will transfer a file from one machine to another in Linux and Windows platforms.
I used ssh libraries (sftp connections) to transfer file to Linux machine.
Now, I wanted to do same for Windows machine. Can someone please help me with this?
Description: To transfer a file from one windows machine(Local) to another windows machine(Server).
Also, I checked with FTP libraries in java, but I wasn't able to create a directory outside the folder created/shared for ftp.
Below is my code I am using currently for ftp.
FTPClient ftpClient = new FTPClient();
FileInputStream inputStream = null;
try {
// pass directory path on server to connect
ftpClient.connect("172.30.17.17");
// pass username and password, returned true if authentication is
// successful
boolean login = ftpClient.login("Administrator", "Password1!");
if (login) {
System.out.println("Connection established...");
inputStream = new FileInputStream("C:/Demo/abcd.txt");
boolean uploaded = ftpClient.storeFile("uploadedFile3.txt",inputStream);
if (uploaded) {
System.out.println("File uploaded successfully !");
} else {
System.out.println("Error in uploading file !");
}
ftpClient.makeDirectory("C:/Demo1"); //Unable to create this here
System.out.println("Folder Created successfully !");
// logout the user, returned true if logout successfully
boolean logout = ftpClient.logout();
if (logout) {
System.out.println("Connection close...");
}
} else {
System.out.println("Connection fail...");
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I am trying to check if the file exist in the FTP. When testing with one user it seems to be fine. But with the scenario of multiple users it seems to throw the below exception :
Exception in thread "main" sun.net.ftp.FtpProtocolException: Welcome message: 421.
Below is the code which we use to check if file is there and we have closed all the connection but still it throws sun.net.ftp.FtpProtocolException:Welcome message: 421.
public boolean getFtpFileExists(String fileUrl)
{
URL theURL = null;
InputStream inputStream = null;
FtpURLConnection ftpUrlConn = null;
boolean ftpFileExists = false;
try
{
theURL = new URL(fileUrl);
ftpUrlConn = (FtpURLConnection)theURL.openConnection();
inputStream = ftpUrlConn.getInputStream();//calling this method will throw a 'FileNotFoundException' if doesn't exist
ftpFileExists = true;
}
catch(FileNotFoundException fnfe)
{
ftpFileExists = false;
}
catch(Exception e)
{
System.out.println(e.toString());
ftpFileExists = false;//hmm, not sure really!
}
finally
{
//close inputStream & connection
if(inputStream != null)
{
try
{
inputStream.close();
}
catch(IOException ioe)
{
System.out.println("Error closing input stream: "+ioe.getMessage());
}
}
try
{
ftpUrlConn.close();
}
catch(Exception e)
{
System.out.println("Error closing ftpUrlConnection");
}
}
return ftpFileExists;
}
could anyone help me please?
Can you recreate the error that you get ?
at any case:
by the error MSG:
421: Service not available, closing control connection. This may be a
reply to any command if the service knows it must shut down.
It sounds like that maybe there's a configuration needs to be done at the FTPServer rather than a code error.
I am trying to insert data into sqlite table but it is giving me exception no such exception.please check my code and let me what changes should i make?
static Connection con;
public static Connection getConnection()
{
try
{
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:/home/zack/another.db");
con.setAutoCommit(false);
System.out.println("Opened database successfully");
}
catch(Exception e)
{
System.out.println("EXCEPTION IN :: SqliteCon:"+e.getMessage());
}
return con;
}
and here is my insert command file.
public String execute()
{
BufferedReader br;
try {
Connection con = SqliteCon.getConnection();
Statement stmt = con.createStatement();
br = new BufferedReader(new FileReader(
"/home/Zack/Desktop/session.csv"));
String line;
while ((line = br.readLine()) != null) {
String[] value = line.split(","); // your seperator
stmt.executeUpdate("INSERT into sessionTab values('jack','pass','NYC'")
//later i will insert values from CSV file
br.close();
}
}
catch (Exception e) {
System.out.println("Exception in UploadData class");
e.printStackTrace();
}
return SUCCESS;
}
I also added jar file to WEB-INF/lib/ of my project
finally solved this issue!!!switched from sqliteman to Firefox sqlite plugin and changed extension of db file from .db to .sqlite.