How can i select a folder inside tomcat to upload an image - java

I'm using Primefaces to upload images to tomcat
through <p:fileUpload/>
I'm uploading in this folder : C:\NEW WORK\sicweb\src\main\webapp\resources\images
But when I put my application on a server, I can't put this way. How can I select a folder through a relative path or something like that
Or put inside C:\java\apache-tomcat-8.0.32\webapps\sicweb ?
This is my class :
public class UploadLogoOrgaoControle implements Serializable {
public static String name = "";
private String caminhoCarregado = "";
private static final long serialVersionUID = 1L;
public void handleFileUpload(FileUploadEvent event) throws IOException {
String path = "C:\\NEW WORK\\sicweb\\src\\main\\webapp\\resources\\images\\";
name = event.getFile().getFileName();
File file = new File(path + name);
caminhoCarregado = "/sicweb/resources/images/" + name;
InputStream is = event.getFile().getInputstream();
OutputStream out = new FileOutputStream(file);
byte buf[] = new byte[1024];
int len;
while ((len = is.read(buf)) > 0)
out.write(buf, 0, len);
is.close();
out.close();
}
public String getCaminhoCarregado() {
return caminhoCarregado;
}
public void setCaminhoCarregado(String caminhoCarregado) {
this.caminhoCarregado = caminhoCarregado;
}
}

Related

Building ZIP files on Android

I am using the following code to build a zip file on my external storage, the only problem is that the file can't be extracted on a Windows PC or be used for anything other than Android, I think I have narrowed the problem to a non-existent folder.
My question is what have I done wrong to cause a weird format of zip?
/**
EXAMPLE USAGE : zipFolder("/sdcard0/downloads", "/sdcard0/Update.zip")
**/
static public void zipFolder(String srcFolder, String destZipFile)
throws Exception
{
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
fileWriter = new FileOutputStream(destZipFile);
zip = new ZipOutputStream(fileWriter);
addFolderToZip("", srcFolder, zip);
zip.flush();
zip.close();
}
static String firstFolder ="";
static private void addFileToZip(String path, String srcFile,
ZipOutputStream zip) throws Exception
{
if (firstFolder == "")
{
firstFolder = getLastPathComponent(path);
}
File folder = new File(srcFile);
if (folder.isDirectory())
{
addFolderToZip(path, srcFile, zip);
}
else
{
byte[] buf = new byte[1024];
int len;
FileInputStream in = new FileInputStream(srcFile);
zip.putNextEntry(new ZipEntry(path.replace(firstFolder, "") + "/" + folder.getName()));
while ((len = in.read(buf)) > 0)
{
zip.write(buf, 0, len);
}
}
}
public static String getLastPathComponent(String filePath)
{
String[] segments = filePath.split("/");
if (segments.length == 0)
return "";
String lastPathComponent = segments[segments.length - 1];
return lastPathComponent;
}
static private void addFolderToZip(String path, String srcFolder,
ZipOutputStream zip) throws Exception
{
File folder = new File(srcFolder);
for (String fileName : folder.list())
{
if (path.equals(""))
{
addFileToZip(folder.getName().replace(firstFolder, ""), srcFolder + "/" + fileName, zip);
}
else
{
addFileToZip(path + "/" + folder.getName(), srcFolder + "/"
+ fileName, zip);
}
}
}

Programmatically creating jar file

I am running Mac OSX Mavericks. Right now I am creating a JAR file from a folder (org, the package). When I use this code from here:
public void run() throws IOException
{
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
JarOutputStream target = new JarOutputStream(new FileOutputStream("/Users/username/Library/Application Support/VSE/temp/output.jar"), manifest);
add(new File("/Users/username/Library/Application Support/VSE/temp/org"), target);
target.close();
}
private void add(File source, JarOutputStream target) throws IOException
{
BufferedInputStream in = null;
try
{
if (source.isDirectory())
{
String name = source.getPath().replace("\\", "/");
if (!name.isEmpty())
{
if (!name.endsWith("/"))
name += "/";
JarEntry entry = new JarEntry(name);
entry.setTime(source.lastModified());
target.putNextEntry(entry);
target.closeEntry();
}
for (File nestedFile: source.listFiles())
add(nestedFile, target);
return;
}
JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
entry.setTime(source.lastModified());
target.putNextEntry(entry);
in = new BufferedInputStream(new FileInputStream(source));
byte[] buffer = new byte[1024];
while (true)
{
int count = in.read(buffer);
if (count == -1)
break;
target.write(buffer, 0, count);
}
target.closeEntry();
}
finally
{
if (in != null)
in.close();
}
}
When I extract the JAR file, There is a META-INF folder, but instead of having the org folder in the extracted jar, I have my Users folder copied into it (except because of it's size, its wasn't filled with all my stuff and my application crashed). I'm expecting this is because the code was written for a Windows system, and the differences with the filesystem (such as \ or /). How would I make the code include only the "org" directory, and not everything leading up to it?
Provided you use Java 7+ you may easily do this by using one of my packages in combination with the zip filesystem provider of the JDK to create it:
private static final Map<String, ?> ENV = Collections.singletonMap("create", "true");
public void run()
throws IOException
{
final Path zipPath = Paths.get("/Users/username/Library/Application Support/VSE/temp/output.jar");
final Path srcdir = Paths.get("/Users/username/Library/Application Support/VSE/temp/org");
final URI uri = URI.create("jar:" + zipPath.toUri());
Files.deleteIfExists(zipPath);
try (
final FileSystem zipfs = FileSystems.newFileSystem(uri, ENV);
) {
copyManifest(zipfs);
copyDirectory(srcdir, zipfs);
}
}
private void copyManifest(final FileSystem zipfs)
throws IOException
{
final Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
Files.createDirectory(zipfs.getPath("META-INF/");
try (
final OutputStream out = Files.newOutputStream(zipfs.getPath("META-INF/MANIFEST.MF"));
) {
manifest.write(out);
}
}
private void copyDirectory(final Path srcdir, final FileSystem zipfs)
{
final String lastName = srcdir.getFileName().toString();
final Path dstDir = zipfs.getPath(lastName);
Files.createDirectory(dstDir);
MoreFiles.copyRecursive(srcDir, dstDir, RecursionMode.FAIL_FAST);
}

Hashing a .raw file shows NullPointerException in Java

I want to hash a file in Java by calling a file that ends with .raw. These are the codes I used:
FileSearch.java
public class FileSearch
{
private static final File file = null;
public static File findfile(File file) throws IOException
{
String drive = (new DetectDrive()).USBDetect();
Path start = FileSystems.getDefault().getPath(drive);
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
#Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
{
if (file.toString().endsWith(".raw"))
{
System.out.println(file);
}
return FileVisitResult.CONTINUE;
}
});
return file;
}
public static void main(String[] args) throws Exception
{
Hash hasher = new Hash();
FileSearch.findfile(file);
try
{
if (file.toString().endsWith("raw"))
{
hasher.hash(file);
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
Hash.java
public class Hash
{
public void hash(File file) throws Exception
{
MessageDigest md = MessageDigest.getInstance("MD5");
FileInputStream fis = new FileInputStream(file);
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = fis.read(dataBytes)) != -1)
{
md.update(dataBytes, 0, nread);
};
byte[] mdbytes = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < mdbytes.length; i++)
{
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
System.out.println("Digest(in hex format):: " + sb.toString());
}
}
The first code is used to find the file and perform hash by running the main method and the second code is the method for hashing the file (by MD5). However, when I run the it gives an ouput:
"name of raw file"
Exception in thread "main" java.lang.NullPointerException at FileSearch.main(FileSearch.java:33)
line 33 is the if (file.toString().endsWith("raw")) portion. Anyone knows how I can fix this?
You never initalize file with anything (Well, you initalize it with null)
private static final File file = null;
So when you call
if (file.toString().endsWith("raw"))
file can only be null.
What you probably want is just
file = FileSearch.findfile(file);
See:
What is a NullPointerException, and how do I fix it?

how to upload file into googlecloud through java

I want to upload a file into bucket in Google cloud storage Api. But when i run the servelet class then it successfully deployed and it shows the output in browser like " Now see here your file content, that you have uploaded on storage..
File uploading done" .
But the problem is the servelet class will not establish the connection to Google cloud storage.And the file will not uploaded into bucket.Once check
the code and give suggestion to how to connect to bucket with this source code.
public class TestCloudStorageServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
private StorageService storage = new StorageService();
private static final int BUFFER_SIZE = 1024 * 1024;
private static final Logger log = Logger.getLogger(TestCloudStorageServlet.class.getName());
#Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
log.info(this.getServletInfo()+" Servlets called....");
resp.setContentType("text/plain");
resp.getWriter().println("Now see here your file content, that you have uploaded on storage..");
ServletFileUpload upload = new ServletFileUpload();
System.out.println(upload);
FileItemIterator iter;
try {
iter = upload.getItemIterator(req);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String fileName = item.getName();
String mime = item.getContentType();
storage.init(fileName, mime);
InputStream is = item.openStream();
byte[] b = new byte[BUFFER_SIZE];
int readBytes = is.read(b, 0, BUFFER_SIZE);
while (readBytes != -1) {
storage.storeFile(b, readBytes);
readBytes = is.read(b, 0, readBytes);
}
is.close();
storage.destroy();
resp.getWriter().println("File uploading done");
//resp.getWriter().println("READ:" + storage.readTextFileOnly(fileName));
log.info(this.getServletName()+" ended....");
}
}
catch (FileUploadException e) {
System.out.println("FileUploadException::"+e.getMessage());
log.severe(this.getServletName()+":FileUploadException::"+e.getMessage());
e.printStackTrace();
} catch (Exception e) {
log.severe(this.getServletName()+":Exception::"+e.getMessage());
System.out.println("Exception::"+e.getMessage());
e.printStackTrace();
}
}
my StorageService class for uploading file.
public class StorageService {
public static final String BUCKET_NAME = "mybucketname";
private FileWriteChannel writeChannel = null;
FileService fileService = FileServiceFactory.getFileService();
private OutputStream os = null;
private static final Logger log = Logger.getLogger(StorageService.class.getName());
public void init(String fileName, String mime) throws Exception {
System.out.println("Storage service:init() method: file name:"+fileName+" and mime:"+mime);
log.info("Storage service:init() method: file name:"+fileName+" and mime:"+mime);
log.info("test..");
GSFileOptionsBuilder builder = new GSFileOptionsBuilder()
.setAcl("public_read")
.setBucket(BUCKET_NAME)
.setKey(fileName)
.setMimeType(mime);
log.info("test..");
AppEngineFile writableFile = fileService.createNewGSFile(builder.build());
boolean lock = true;
writeChannel = fileService.openWriteChannel(writableFile, lock);
os = Channels.newOutputStream(writeChannel);
}
public void storeFile(byte[] b, int readSize) throws Exception {
os.write(b, 0, readSize);
os.flush();
}
public void destroy() throws Exception {
log.info("Storage service: destroy() method");
os.close();
writeChannel.closeFinally();
}
}

Java Large file to be saved in database - Object design

I am trying to figure out object design to implement large file(~600 MB) respository in the Database using hibernate.
Please suggest a correct approach/design?
class ModelClass{
String name; //meta data
...
Option 1.
byte[] file; // dont want to load the content of the entire file
// in memory by using this but hibernate recognizes
// this datatype
Option 2.
InputStream inputStream;
OutputStream outputStream;
// I can have the methods to provide the input or output stream
// but i dont think its a clean approach. I am not sure how
// I will be able to work with hibernate with streams
Option 3.
File fileHandle;
}
Any other options??
I would like to call save(Object) method of hibernateTemplate to save the object in Database. Dont know if I should have just the meta-data in the class and handle the file save and retreive seperately.
Thanks in advance.
Another workable solution is to use "Work" Interface. The purpose was to avoid loading the file content into memory.
session.doWork(new Work(){
#Override
public void execute(Connection conn) {
//direct sql queries go here
}
});
I have written a SerializableFile class that keeps data in a file. When the object is read, it creates a temporary file.
Here it is:
public class SerializableFile implements Serializable {
private static final File TEMP_DIR = getTempDir();
private transient boolean temporary;
private transient String name;
private transient File file;
public SerializableFile() {
}
public SerializableFile(File file) {
this.file = file;
this.name = file.getName();
this.temporary = false;
}
#Override
protected void finalize() throws Throwable {
dispose();
super.finalize();
}
public void dispose() {
if (temporary && file != null) {
file.delete();
file = null;
}
}
public File keep(String name) throws IOException {
if (temporary) {
temporary = false;
} else {
File newFile = new File(TEMP_DIR, name);
keepAs(newFile);
file = newFile;
}
return file;
}
public void keepAs(File outFile) throws IOException {
if ((temporary || file.equals(outFile)) && file.renameTo(outFile)) {
temporary = false;
file = outFile;
} else {
InputStream in = new FileInputStream(file);
try {
OutputStream out = new FileOutputStream(outFile);
try {
byte buf[] = new byte[4096];
for (int n = in.read(buf); n > 0; n = in.read(buf)) {
out.write(buf, 0, n);
}
} finally {
out.close();
}
} finally {
in.close();
}
outFile.setLastModified(file.lastModified());
}
}
public String getName() {
return name;
}
public File getFile() {
return file;
}
public long lastModified() {
return file.lastModified();
}
private void writeObject(ObjectOutputStream out) throws IOException {
int size = (int)file.length();
long date = file.lastModified();
out.writeUTF(name);
out.writeInt(size);
out.writeLong(date);
InputStream in = new FileInputStream(file);
try {
byte buf[] = new byte[4096];
while (size > 0) {
int n = in.read(buf);
if (n <= 0 || n > size) {
throw new IOException("Unexpected file size");
}
out.write(buf, 0, n);
size -= n;
}
} finally {
in.close();
}
}
private void readObject(ObjectInputStream in) throws IOException {
name = in.readUTF();
int size = in.readInt();
long date = in.readLong();
file = File.createTempFile("tmp", ".tmp", TEMP_DIR);
OutputStream out = new FileOutputStream(file);
try {
byte buf[] = new byte[4096];
while (size > 0) {
int n = in.read(buf, 0, size <= buf.length ? size : buf.length);
if (n <= 0 || n > size) {
throw new IOException("Unexpected file size");
}
out.write(buf, 0, n);
size -= n;
}
} finally {
out.close();
}
file.setLastModified(date);
temporary = true;
}
private static File getTempDir() {
File dir;
String temp = System.getProperty("com.lagalerie.live.temp-dir");
if (temp != null) {
dir = new File(temp);
} else {
String home = System.getProperty("user.home");
dir = new File(home, "temp");
}
if (!dir.isDirectory() && !dir.mkdirs()) {
throw new RuntimeException("Could not create temp dir " + dir);
}
return dir;
}
}
Open JPA supports a #Persistent annotation with some databases:
MySQL
Oracle
PostgreSQL
SQL Server
DB2
Even if you are still using an RDBMS as a data store, you should consider storing this binary data into a file system, and saving the directory / location of the path into the database, instead of storing this as a BLOB or CLOB into the database.

Categories