How to upload Multiple files using play framework? - java

i am using play framework 2.1.2 using java and i am creating view to upload multiple files and my code is here :
#form(action = routes.upload.up, 'enctype -> "multipart/form-data") {
<input type="file" name="picture" accept="application/pdf" multiple="multiple"><br/>
<input type="submit" value="upload">
}
i want to upload only doc and pdf file.
how to restrict form to upload only doc and pdf file ?
i can this with java but i am looking for html code.
after this i want to store multiple file to permanent storage in my computer.
and print name of file i uploaded.
my code :
public static Result up(){
MultipartFormData md=request().body().asMultipartFormData();
List<FilePart>file;
file=md.getFiles();
for(FilePart p: file){
Logger.info(p.getFilename());
}
return ok(file.get(0).getFilename());
}
it is storing file into temp directory but i want to store to permanent location not on temp directory as a file not temp file like if i upload a.docx i want to store this file into storage with a.docx name.
i don't want to store file into database.
and how to list all file that i uploaded by file name?
i found some question but i am not getting that answers because that question is for old version.
give me some idea to fix this issue.

Here is how I implemented mine. I apologize if I made any mistakes somewhere. I "refactored" it so that it doesn't look anything like my production code.
In HTML I have:
<form name="fileUploadForm" method="POST" enctype="multipart/form-data" action="#routes.Application.upload()">
File 1: <br /> <input type="file" name="filePart1" id="filePart1"><br />
File 2: <br /> <input type="file" name="filePart2" id="filePart1"><br />
</form>
In my controller I have:
public static Result upload() {
MultipartFormData body = request().body().asMultipartFormData();
FilePart filePart1 = body.getFile("filePart1");
FilePart filePart2 = body.getFile("filePart2");
File newFile1 = new File("path in computer");
File newFile2 = new File("path in computer");
File file1 = filePart1.getFile();
File file2 = filePart2.getFile();
InputStream isFile1 = new FileInputStream(file1);
InputStream isFile2 = new FileInputStream(file2);
byte[] byteFile1 = IOUtils.toByteArray(isFile1);
byte[] byteFile2 = IOUtils.toByteArray(isFile2);
FileUtils.writeByteArrayToFile(newFile1, byteFile1);
FileUtils.writeByteArrayToFile(newFile2, byteFile2);
isFile1.close();
isFile2.close();
}
Just like Kris said, you will have to get Apache's CommonIO
You can easily do this buy adding this into your Build.scala found in /PlayProject/project:
import sbt._
import Keys._
import play.Project._
import com.typesafe.config._
object ApplicationBuild extends Build {
val appDependencies = Seq(
"commons-io" % "commons-io" % "2.4" //add this here
)
}
In this implementation, you can store the files anywhere on your computer where you specified in File newFile1. But you will have to use a database if you want to list your files. But you only have to store the file path as a String (varchar) in the database. I will leave that part up to you to figure out as I don't know how you want to handle file retrieval.
You can restrict user to only upload certain type of files by using Javascript. Have Javascript do form validation by checking the file name: Here is an example:
<script>
var file1 = document.getElementById("filePart1").value;
if (file1.indexOf(".pdf") == -1) {
alert("Not a PDF file!");
else {
document.fileUploadForm.submit();
}
</script>
Hope all of that helps.

To get your file somewhere else than temp location you would need to copy it, best using something like FileUtils from apache commons (http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html)
As for the file names you can get it from MultipartFormData
First getFiles() method
Second getFile():File method

One way could be is to use a custom body parser that can take a specified directory and save the uploaded files there. But you have to add some Scala code to the project. Here is the gist:
https://gist.github.com/nraychaudhuri/6349808

Related

Java + FIle Upload + Pulpload

We have a legacy system and user wants to change the file upload page.I looked into pulpload and implemented the UI changes. Now for our java changes we are using an in house framework with web macro and it works like this
<form method="post" name="transmitter" action="$url" enctype="multipart/form-data">
..<INPUT TYPE="FILE" name="path_and_filename" id="path_and_filename" size="50">...
</form>
Java Code:
MultipartDocument uploadedFile = (MultipartDocument) data.get("path_and_filename");
Here this MultipartDocument is part of jar which give us all the info we needed for file uploaded like mimetype, docData, docName etc.
Issue: Pulpload doesn't uses input type= file so how are we suppose to do this, as we can't change the Java code [aslots of other pages are using the same framework].

How can I get selected file path from "file" tag?

My file file storage location is "E:\Records\DOCS\test.html"
How can I get Selected file path, following is my jsp tag
<form:input path="FileData" type="file"/>
I am selecting the test.html from above path and getting the selected file my model class as follows, I am able to get selected file name but along with file name I need full path.
How can I get full path in my model class ?
private CommonsMultipartFile fileData;
public CommonsMultipartFile getFileData()
{
return fileData;
}
public void setFileData(CommonsMultipartFile fileData)
{
System.out.println(fileData.getOriginalFilename()); // it gives output as test.html, I need full path
return fileData;
}
Filepaths of files selected in an <input type='file' /> tags are on the client-side. You don't need to know the filepath on the client-side, and the browser protects the client by preventing you from reading the path in any way in Javascript, and by not sending it in the request at all. All you should be concerned with is where you are going to save the file on the server-side, and you obviously would not want to save it to the same place as where it was on the client (as this would give the user the power to overwrite any file on your server they wanted). So you don't need to read any path.

Java Servlet JSP delete file using apache commons FileUpload

I have been searching this topic for quite a while, and haven't found anything that has been able to solve my problem.. so I turn to you!
I have a JSP where I open a file dialog box to select a file. Previously, I used this to upload the file to a specified directory (in my code). This works fine. I am now trying to use the same code to delete that same file by selecting it in the appropriate directory and passing it off to the servlet, which I included below. I am using the Apache Common FileUpload library to do this.
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// checks if the request actually contains upload file
if (!ServletFileUpload.isMultipartContent(request)) {
// if not, we stop here
return;
}
// configures some settings
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload delete = new ServletFileUpload(factory);
// constructs the directory path to delete file
String deletePath = UPLOAD_DIRECTORY;
// parses the request's content to extract file data
List formItems = delete.parseRequest(request);
Iterator iter = formItems.iterator();
// iterates over form's fields
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
// processes only fields that are not form fields
if (!item.isFormField()) {
String fileName = new File(item.getName()).getName();
String filePath = deletePath + File.separator + fileName;
File storeFile = new File(filePath);
//File storeFile = new File("C:\\temp\\discLogo.txt");
// deletes the file on disk
boolean erased = storeFile.delete();
}
}
UPLOAD_DIRECTORY is where I am storing my files from my upload JSP. The delete method works fine if I uncomment the line I commented out for storeFile with the hardcoded directory, as long as I select a DIFFERENT FILE in the directory initially. This leads me to believe the HttpServletRequest is holding the file in memory somewhere.
Is this correct? is there any way I can release it so I can delete the file I select initially? Or is there a much simpler way to do this?
Thanks!
The File#delete() will return false if the file does not exist at all (use File#exists() to test it beforehand), or if the file is locked because it is been opened by another app or even your own code!
Provided that this file is written do disk beforehand by your own code and guaranteed not opened elsewhere, then you should ensure that you're invoking OutputStream#close() in the finally block after writing the file's content. This problem suggests that you didn't. If you leave the file open after writing to it, then it cannot be deleted until you restart the server/JVM.
Apache Commons IO, which you should already have as a dependency of FileUpload, comes with handy utility methods reducing the file copy and close boilerplate.
InputStream input = item.getInputStream();
OutputStream output = new FileOutputStream(storeFile);
try {
IOUtils.copy(input, output);
} finally {
IOUtils.closeQuietly(output);
IOUtils.closeQuietly(input);
}
See also:
How I save and retrieve an image on my server in a java webapp.
What is the object of uploading the file? Are you going to check all the files by content?
I would say the simpler way would be to not upload the whole file. What you want to do is to call some code - JSP/Servlet -, pass as a parameter an ID of the file (most usually the path inside the server) and make it delete the file. If all the files uploaded are in the same folder, then the name should be enough (*).
After all, by uploading the file you are forcing you to have a copy in your PC of the file you want to delete (what if you deleted your local file? Should you not be able to delete the file in the server?)
(*) be sure to perform safety checks so nobody can pass ..\..\WEB-INF\web.xml as a parameter.
you can delete the file because the platform indepenedent solution is like this
File deleteFile = new File(<file Path> ) ;
// check if the file present or not
if( deleteFile.exists() )
deleteFile.delete() ;
Be carefule if the file path is directory then the directory must empty before deleting.
UploadFileorDelete
Looks like my issue was in my JSP. I had the following form initially:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Delete</title>
</head>
<body>
<form method="post" action="DeleteServlet"
enctype="multipart/form-data">
Select file to delete: <input type="file" name="dataFile"
id="fileChooser" /><br />
<br /> <input type="submit" value="Delete" />
</form>
</body>
</html>
I changed this by deleting encytype="multipart/form-data" from the form, and was then able to use request.getParameter("dataFile") to get the file name.

Upload PDF to database and storage system (folder) in JSP

Just like title, I want to ask, how to adding pdf files with upload form to my storage folder (e.g: uploadData) then its added to database as a file too in JSP.
If it not possible, it's ok to added to database as a text.
If it possible as a file, what type of my table for that pdf? blob? or text?
I accept blog links/other links that relevant for my problem
sorry for bad english.
Servlet 3.0 container's has standard support for multipart data. It also has the support to write to local file system. First you should be writing a HTML page which takes the file input along with other input parameters.
<form action="uploadservlet" method="post" enctype="multipart/form-data">
<input type="text" name="name" />
<input type="text" name="age" />
<input type="file" name="photo" />
<input type="submit" />
</form>
Now write a UploadServlet which uses the Servlet 3.0 Upload API. Here is the code which demonstrates the usage of API. Fist the servlet handling multipart data should define MultiPartConfig using any of the two approaches:
#MultiPartConfig annotation on Servlet Class
In web.xml, by adding entry inside definition.
Here is the UploadServlet,
#MultipartConfig
public class UploadServlet extends HttpServlet
{
protected void service(HttpServletRequest request,
HttpServletResponse responst) throws ServletException, IOException
{
Collection<Part> parts = request.getParts();
if (parts.size() != 3) {
//can write error page saying all details are not entered
}
Part filePart = httpServletRequest.getPart("photo");
InputStream imageInputStream = filePart.getInputStream();
//read imageInputStream
filePart.write("somefiepath");
//can also write the photo to local storage
//Read Name, String Type
Part namePart = request.getPart("name");
if(namePart.getSize() > 20){
//write name cannot exceed 20 chars
}
//use nameInputStream if required
InputStream nameInputStream = namePart.getInputStream();
//name , String type can also obtained using Request parameter
String nameParameter = request.getParameter("name");
//Similialrly can read age properties
Part agePart = request.getPart("age");
int ageParameter = Integer.parseInt(request.getParameter("age"));
}
}
If you are not using Sevlet 3.0 Container, you should be truing Apache Commons File Upload. Here are the links for using Apache Commons File Upload:
Using Apache Commons File Upload
Apache Commons File Upload Example
References:
File Upload Using Servlet 3.0
Servlet 3.0 javax.servlet.http.HttpServletRequest API
Servlet 3.0 javax.servlet.http.Part API
Uploading File using Servlet 3.0
The easiest way I know of to handle file uploads is by using Commons FileUpload. The documentation gives you a step by step overview of how to accept uploaded files, including how to easily copy them into a file.
Should you decide to put the PDF in a database (I wouldn't do that), BLOB is your best choice, a PDF file isn't text.
I would however advise not to cram all that logic in a JSP but in a servlet instead.

Java SE HTML POST filename issue

I'm currently writing a program using nanoHTTPD to upload files to a server. Currently I can successfully upload the file with a preassigned name. However, I would like to maintain the original name of the file being uploaded.
How it uploads now:
Original filename: foo.jpg
Uploaded filename: file123.whatever
How I want it to upload:
Original file name: foo.jpg
Uploaded filename: foo.jpg
Here is the HTML being used:
<FORM ENCTYPE="multipart/form-data" ACTION="/uploaded.html" METHOD=POST>
Upload a file: <INPUT NAME="userfile1" TYPE="file">
<INPUT TYPE="submit" VALUE="Send File">
</FORM>
Here is the vanilla nanoHTTPD code for reference: http://pastebin.com/pMNS3VKf
Note: I would prefer to use Java SE and avoid Java EE.
Any advice would be welcomed, thank you.
Edit: in short all I need to learn how to do is get the filename from the HTML POST.
I'm not certain if your NanoHTTPD is the same as mine, but with mine you can retrieve the original filename by looking at the parms Parameters object for the same key that you retrieve the file from the files Parameters object.
Enumeration<Object> keys = files.keys()
while (keys.hasMoreElements())
{
String key = keys.nextElement().toString();
String origFileName = parms.getProperty(key);
String fsFileName = files.getProperty(key);
this.renameFile(fsFileName, origFileName);
}
Once you retrieve the filename, you can use whatever method you want to rename the file. (With proper checking. It could be a huge security hole if you completely trusted their original filename.)

Categories

Resources