Skip to content

Instantly share code, notes, and snippets.

@allixender
Created August 22, 2016 00:49
Show Gist options
  • Save allixender/5e10c5eca2ed3efbb196a9d4e37ee215 to your computer and use it in GitHub Desktop.
Save allixender/5e10c5eca2ed3efbb196a9d4e37ee215 to your computer and use it in GitHub Desktop.
package controllers;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import play.Logger;
import play.mvc.Controller;
import play.mvc.Http.MultipartFormData;
import play.mvc.Result;
public class FileHandling extends Controller {
// private final static String defLang = "en";
private final static String userName = "guest";
private final static String fileFolder = play.Play.application()
.configuration().getString("filedb.localfolder");
public static Result index() {
final String lang = Application.getLang(session("lang"));
return ok(views.html.fileindex.render("filehandling basurl test", lang,
userName));
}
public static Result handleUpload() {
final String lang = Application.getLang(session("lang"));
MultipartFormData body = request().body().asMultipartFormData();
MultipartFormData.FilePart picture = body.getFile("picture");
if (picture != null) {
String fileName = picture.getFilename();
// FB DEAD String contentType = picture.getContentType();
File file = picture.getFile();
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File savedFile = new File(fileFolder + fileName);
if (savedFile.exists()) {
return ok(views.html.fileindex.render(
"error, file exists already", lang, userName));
} else {
boolean retval = savedFile.createNewFile();
if (!retval) {
Logger.error("couldn't create/save file "
+ savedFile.getAbsolutePath());
}
fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); // no doubt here is 0
// Writes len bytes from the specified byte array
// starting at offset off to this byte array output
// stream.
}
byte[] bytes = bos.toByteArray();
fos = new FileOutputStream(savedFile);
fos.write(bytes);
fos.flush();
fos.close();
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
return ok(views.html.fileindex.render("error IOException",
lang, userName));
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return ok(views.html.fileindex.render("could have worked", lang,
userName));
} else {
return ok(views.html.fileindex.render("error, Missing file", lang,
userName));
// flash("error", "Missing file");
// return redirect(routes.FileHandling.index());
}
}
public static Result handleDirectUpload() {
final String lang = Application.getLang(session("lang"));
File file = request().body().asRaw().asFile();
FileInputStream fis = null;
FileOutputStream fos = null;
try {
String fileName = file.getName();
File savedFile = new File(fileFolder + fileName);
if (savedFile.exists()) {
return ok(views.html.fileindex.render(
"error, file exists already", lang, userName));
} else {
boolean retval = savedFile.createNewFile();
if (!retval) {
Logger.error("couldn't create/save file "
+ savedFile.getAbsolutePath());
}
fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); // no doubt here is 0
// Writes len bytes from the specified byte array starting
// at offset off to this byte array output stream.
}
byte[] bytes = bos.toByteArray();
fos = new FileOutputStream(savedFile);
fos.write(bytes);
fos.flush();
fos.close();
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
return ok(views.html.fileindex.render("error IOException", lang,
userName));
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return ok("could have worked");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment