Here is one use case for need of files stored on the cloud - the third party API you use takes URL as a parameter to process a picture :-)
I wanted to build a little game to communicate emotion long distance via BB8 toy, we saw it in the class, I found a perfect API that would give me coordinates of mouth, eyes, nose, etc, and then I use math equations to process the curvature of the mouth to detect the person's emotional state. (sometimes math does help!)
https://www.kairos.com/docs/api/#face-recognition
I used the code below to store my files on heroku then pass to Kairos
The video below shows the end result
I wanted to build a little game to communicate emotion long distance via BB8 toy, we saw it in the class, I found a perfect API that would give me coordinates of mouth, eyes, nose, etc, and then I use math equations to process the curvature of the mouth to detect the person's emotional state. (sometimes math does help!)
https://www.kairos.com/docs/api/#face-recognition
I used the code below to store my files on heroku then pass to Kairos
The video below shows the end result
storing files in postgres
Create your table in Postgres
CREATE TABLE FILES(filename text, file bytea);
Save file to Postgres:
CREATE TABLE FILES(filename text, file bytea);
Save file to Postgres:
package command;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import connectionprovider.ConnectionProvider;
public class SaveFileToDBCommand {
public void execute(String fileName, InputStream fis, long l) {
try {
ByteArrayOutputStream output = new ByteArrayOutputStream();
IOUtils.copy(fis, output);
byte[] filecontent = output.toByteArray();
Connection connection = ConnectionProvider.getConnection();
PreparedStatement stmt = connection
.prepareStatement("INSERT INTO FILES VALUES (?, ?)");
stmt.setString(1, fileName);
stmt.setBytes(2, filecontent);
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Retrieve the file from Postgres
package command;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import connectionprovider.ConnectionProvider;
public class GetFileFromPOSTGRESCommand {
public InputStream execute(String filename) {
Connection conn;
try {
conn = ConnectionProvider.getConnection();
PreparedStatement stmt = conn
.prepareStatement("SELECT FILE FROM FILES WHERE FILENAME=?");
stmt.setString(1, filename);
ResultSet rs = stmt.executeQuery();
if (rs != null) {
while (rs.next()) {
InputStream is = rs.getBinaryStream("file");
return is;
}
rs.close();
}
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
Services:
@GET
@Path("download/{filename}")
@Produces(MediaType.WILDCARD)
public Response getFile(@PathParam("filename") String filename) {
try {
GetFileFromPOSTGRESCommand getFile = new GetFileFromPOSTGRESCommand();
InputStream is = getFile.execute(filename);
ResponseBuilder response = Response.ok((Object) is);
response.header("Content-Disposition", "attachment; filename=\""
+ filename + "\"");
return response.build();
} catch (Exception e) {
return Response.status(404).entity(e.getMessage()).build();
}
}
@GET
@Path("inline/{filename}")
@Produces("image/*")
public Response renderFile(@PathParam("filename") String filename) {
try {
GetFileFromPOSTGRESCommand getFile = new GetFileFromPOSTGRESCommand();
InputStream is = getFile.execute(filename);
ResponseBuilder response = Response.ok((Object) is);
response.header("Content-Disposition", "inline; filename=\""
+ filename + "\"");
return response.build();
} catch (Exception e) {
return Response.status(404).entity(e.getMessage()).build();
}
}
@POST
@Path("/upload/db")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadDBFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
SaveFileToDBCommand cmd = new SaveFileToDBCommand();
cmd.execute(fileDetail.getFileName(), uploadedInputStream, fileDetail.getSize());
return Response.status(200).build();
}