[英]How to store uploaded mulitple pdf file to a specific location in java?
i want to store uploaded file in a specific location in java. 我想将上传的文件存储在Java中的特定位置。 if i upload a.pdf
then i want it to store this at "/home/rahul/doc/upload/"
. 如果我上传a.pdf
那么我希望它将其存储在"/home/rahul/doc/upload/"
。 i went through some questions and answers of stack overflow but i am not satisfied with solutions. 我经历了一些堆栈溢出的问题和答案,但对解决方案不满意。
i am working with Play Framework 2.1.2
. 我正在使用Play Framework 2.1.2
。 i am not working with servlet
. 我不使用servlet
。
i am uploading but it is storing file into temp directory but i want that file store into a folder as not a temp file i want that file like a.pdf in folder not like temp file. 我正在上传,但它正在将文件存储到temp目录中,但我希望将该文件存储到一个文件夹中而不是一个临时文件中,我希望该文件像a.pdf一样位于文件夹中,而不是像temp文件一样。
public static Result upload() {
MultipartFormData body = request().body().asMultipartFormData();
FilePart filePart1 = body.getFile("filePart1");
File newFile1 = new File("path in computer");
File file1 = filePart1.getFile();
InputStream isFile1 = new FileInputStream(file1);
byte[] byteFile1 = IOUtils.toByteArray(isFile1);
FileUtils.writeByteArrayToFile(newFile1, byteFile1);
isFile1.close();
}
but i am not satisfied with this solution and i am uploading multiple doc files. 但我对这种解决方案不满意,我正在上传多个doc文件。
for eg. 例如 i upload one doc ab.docx then after upload it is storing temp directory and file is this: 我上传了一个文档ab.docx,然后上传后存储了temp目录,文件是这样的:
and it's location is this: /tmp/multipartBody5886394566842144137asTemporaryFile
它的位置是这样的: /tmp/multipartBody5886394566842144137asTemporaryFile
but i want this: /upload/ab.docx
但我想要这个: /upload/ab.docx
tell me some solution to fix this. 告诉我一些解决方案。
Everything's correct as a last step you need to renameTo
the temporary file into your upload folder, you don't need to play around the streams it's as simple as: 一切都正确,这是您需要重renameTo
的最后一步。将临时文件复制到您的上传文件夹中,您无需在流中播放即可,它非常简单:
public static Result upload() {
Http.MultipartFormData body = request().body().asMultipartFormData();
FilePart upload = body.getFile("picture");
if (upload != null) {
String targetPath = "/your/target/upload-dir/" + upload.getFilename();
upload.getFile().renameTo(new File(targetPath));
return ok("File saved in " + targetPath);
} else {
return badRequest("Something Wrong");
}
}
BTW you should implement some checking if targetPath doesn't exist to prevent errors and/or overwrites. 顺便说一句,您应该实施一些检查,以确定targetPath是否不存在以防止错误和/或覆盖。 Typical approach is incrementing the file name if file with the same name already exists, for an example sending a.pdf
three times should save the files as a.pdf
, a_01.pdf
, a_02.pdf
, etc. 如果已经存在具有相同名称的文件,则典型的方法是增加文件名,例如,发送3次a.pdf
的示例应将文件另存为a.pdf
, a_01.pdf
, a_02.pdf
等。
i just completed it. 我刚完成。 My solution is working fine. 我的解决方案工作正常。
My solution of uploading multiple files is : 我上传多个文件的解决方案是:
public static Result up() throws IOException{
MultipartFormData body = request().body().asMultipartFormData();
List<FilePart> resourceFiles=body.getFiles();
InputStream input;
OutputStream output;
File part1;
String prefix,suffix;
for (FilePart picture:resourceFiles) {
part1 =picture.getFile();
input= new FileInputStream(part1);
prefix = FilenameUtils.getBaseName(picture.getFilename());
suffix = FilenameUtils.getExtension(picture.getFilename());
part1=new File("/home/rahul/Documents/upload",prefix+"."+suffix);
part1.createNewFile();
output = new FileOutputStream(part1);
IOUtils.copy(input, output);
Logger.info("Uploaded file successfully saved in " + part1.getAbsolutePath());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.