push files

This commit is contained in:
2024-05-07 11:47:54 -05:00
parent 75cd74930c
commit 0264594936
124 changed files with 3240 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_j2eeVersion>10-web</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_j2eeVersion>
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>gfv700ee10</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>
<org-netbeans-modules-projectapi.jsf_2e_language>JSP</org-netbeans-modules-projectapi.jsf_2e_language>
<org-netbeans-modules-maven-jaxws.rest_2e_config_2e_type>ide</org-netbeans-modules-maven-jaxws.rest_2e_config_2e_type>
</properties>
</project-shared-configuration>

View File

@@ -0,0 +1,53 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>asdv</groupId>
<artifactId>LabFileUpload_Dbase</artifactId>
<version>1</version>
<packaging>war</packaging>
<name>LabFileUpload_Dbase</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jakartaee>10.0.0</jakartaee>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>${jakartaee}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>12.0.0</version>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,13 @@
package asdv.labfileupload;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;
/**
* Configures Jakarta RESTful Web Services for the application.
* @author Juneau
*/
@ApplicationPath("resources")
public class JakartaRestConfiguration extends Application {
}

View File

@@ -0,0 +1,20 @@
package asdv.labfileupload.resources;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Response;
/**
*
* @author
*/
@Path("jakartaee10")
public class JakartaEE10Resource {
@GET
public Response ping(){
return Response
.ok("ping Jakarta EE")
.build();
}
}

View File

@@ -0,0 +1,122 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package beans;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
*
* @author ASDV1
*/
public class Database
{
public static int saveFile( String directory, String fileName, String extension)
{
int count = -1;
File f = null;
Connection con = connection();
if (con == null)
{
System.out.println("cannot connect to database");
return -1;
}
try
{
PreparedStatement ps = null;
String sqlStr = "INSERT INTO binary_files (file_name , file_extension, size_kb, binary_data )VALUES(?,?,?,?)";
ps = con.prepareStatement(sqlStr);
Path path = Paths.get(directory + "/" + fileName);
f = path.toFile();
long fileLength = f.length();
ps.setString(1, fileName);
ps.setString(2, extension);
ps.setLong(3, fileLength/1000);
FileInputStream fis = new FileInputStream(f);
ps.setBinaryStream(4, fis, fileLength);
//execute
count = ps.executeUpdate();
}
catch ( FileNotFoundException e )
{
System.out.println("method Database:saveFile " + e);
}
catch (SQLException e)
{
System.out.println("method Database:saveFile " + e);
}
finally
{
try
{
if ( f!= null )
f.delete();//delete the uploaded file
con.close();
}
catch (SQLException e)
{
System.out.println("connection did not close...method Database:saveFile " + e);
return -1;
}
}
return count;
}
private static Connection connection() //throws InstantiationException, IllegalAccessException
{
String databaseName = "downloads";
String userName = "java";
String password = "java";
String URL2 = "com.mysql.jdbc.Driver";
Connection con = null;
try
{// Load Sun's jdbc driver
Class.forName(URL2).newInstance();
System.out.println("JDBC Driver loaded!");
}
catch (Exception e) // driver not found
{
System.err.println("Unable to load database driver");
System.err.println("Details : " + e);
return null;
}
String ip = "localhost"; //internet connection
String url = "jdbc:mysql://" + ip + ":3306/" + databaseName;
url += "?allowPublicKeyRetrieval=true&useSSL=false";
try
{
con = DriverManager.getConnection(url, userName, password);
con.setReadOnly(false);
}
catch (Exception e)
{
System.err.println(e.toString());
return null;
}
System.out.println("connection successfull");
return con;
}
public static void main(String[] args)
throws SQLException
{
connection();
}
}

View File

@@ -0,0 +1,197 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/JSF/JSFManagedBean.java to edit this template
*/
package beans;
import jakarta.enterprise.context.SessionScoped;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Named;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.file.UploadedFile;
/**
*
* @author asdv5
*/
@Named(value = "fileUploadBean")
@SessionScoped
public class FileUploadBean implements Serializable
{
private UploadedFile file;
public void handleFileUpload(FileUploadEvent event)
{
System.out.println("handleFileUpload");
UploadedFile file = event.getFile();
if (uploadToDatabase(file))
{
FacesContext fc = FacesContext.getCurrentInstance();
fc.addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO,
"upload successful",
file.getFileName() + " is uploaded."));
}
else
{
FacesContext fc = FacesContext.getCurrentInstance();
fc.addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Failed to upload.",
file.getFileName() + " was not uploaded."));
}
}
public static boolean fileExists(Path path, String fileName)
{
Path p = Paths.get(path.toString() + "/" + fileName);
System.out.println(p);
if (Files.exists(p))
{
return true;
}
return false;
}
/**
* Writes the file in Uploaded file into MAC existing directory
* /Users/asdv5/user1")
*
* @param f the file to be uploaded.
* @return the filename written, or null if nothing was written
*/
private String writeToAnyExistingDirectory(UploadedFile f)
{
InputStream input = null;
Path path = Paths.get("/media/DataEXT4/WebDev/");
String s = "";
String fileName = "";
if (Files.exists(path))
{
s = path.toString();
}
try
{
input = f.getInputStream();
fileName = f.getFileName();
Files.copy(input, new File(s, fileName).toPath());
System.out.println("---success saving file" + s + " " + fileName);
}
catch (FileAlreadyExistsException e)
{
int i = 1;
do
{
fileName = f.getFileName();
int indexOfLastDot = fileName.lastIndexOf(".");
if (indexOfLastDot != -1)
{
String s1 = fileName.substring(0, indexOfLastDot);
s1 += "_" + i;
s1 += fileName.substring(indexOfLastDot);
fileName = s1;
++i;
}
else
{
fileName += "_1";
}
}
while (fileExists(path, fileName));
try
{
input.close();
input = f.getInputStream();
Files.copy(input, new File(s, fileName).toPath());
System.out.println("---success saving file" + s + " " + fileName);
}
catch (IOException ex)
{
System.out.println(ex);
return null;
}
return fileName;
}
catch (IOException e)
{
System.out.println("---failed to upload file " + e);
return null;
}
finally
{
try
{
input.close();
}
catch (IOException e)
{
System.out.println(e);
return null;
}
}
return fileName;
}
/**
* Uploads a file to a database. Firstly writes the file to the user's
* directory and then copies it to the database and deletes it from the
* user's directory.
*
* @param f Uploaded file that contains the file to be uploaded
* @return true if the file was uploaded, false otherwise.
*/
private boolean uploadToDatabase(UploadedFile f)
{
//users's directory
String uploadDirectory = "/media/DataEXT4/WebDev/";
//>if failed to write to user's directory, return false
if (writeToAnyExistingDirectory(f) == null)
{
return false;
}
//>separate file name from file extension if any
String fileName = f.getFileName();
int indexOfDot = fileName.lastIndexOf(".");
String fileExtension = "";
if (indexOfDot != -1)
{
fileExtension = fileName.substring(indexOfDot);
}
String fullPathNfileName = uploadDirectory + "/" + fileName;
//> write to database
int count = Database.saveFile(uploadDirectory, fileName, fileExtension);
System.out.println(" ...the database server returned: " + Integer.toString(count));
Path path = Paths.get(uploadDirectory + "/" + fileName);
//>delete file from usr's directory
try
{
Files.deleteIfExists(path);
}
catch (IOException e)
{
FacesContext fc = FacesContext.getCurrentInstance();
fc.addMessage(null,
new FacesMessage(
FacesMessage.SEVERITY_WARN,
file.getFileName(),
"Failed to delete an intemmediate file but the file was uploaded to database. "
)
);
}
return count >= 0 ? true : false;
}
}

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="3.0" xmlns="https://jakarta.ee/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd">
<!-- Define Persistence Unit -->
<persistence-unit name="my_persistence_unit">
</persistence-unit>
</persistence>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd"
bean-discovery-mode="all">
</beans>

View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
<class-loader delegate="true"/>
<jsp-config>
<property name="keepgenerated" value="true">
<description>Keep a copy of the generated servlet class' java code.</description>
</property>
</jsp-config>
</glassfish-web-app>

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="6.0" xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd">
<context-param>
<param-name>jakarta.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>jakarta.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>

View File

@@ -0,0 +1,32 @@
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<p:growl id="localForm1Growl" showDetail="true" life="3000"
redisplay="true" />
<p:fileUpload id="upload" value="#{fileUploadBean}"
mode="advanced"
process="@this"
listener="#{fileUploadBean.handleFileUpload}"
dragDropSupport="true"
update="localForm1Growl"
label="Add a file"
uploadLabel=" upload all"
cancelLabel="discard all"
>
<p:dataTable >
<p:column>
</p:column >
</p:dataTable>
</p:fileUpload>
</h:form>
</h:body>
</html>