2016-11-27 2 views
1

Я хочу загрузить файл/изображение на свой сайт. На самом деле я могу загрузить свой файл, но иногда у меня есть ошибка на моей странице, и я думаю, что это не лучший способ сделать это.PlayFramework - Как загрузить файл/изображение

Мой код:

private String uploadImage() 
{ 
    String result = f.validate("avatar"); 
    if(result.isEmpty()) 
     return "none"; 
    Http.MultipartFormData.FilePart<File> file = body.getFile("avatar"); 

    if(file == null) 
     return "File is empty or not exist."; 

    String filename = file.getFilename(); 

    /* Debug file file*/ 
    Logger.debug("nameFile " + file.getFilename()); 
    Logger.debug("contentFile " + file.getContentType()); 
    /* End debug*/ 

    File f = file.getFile(); 

    /* Debug file f*/ 
    String name = f.getName(); 
    long totalSpace = f.getTotalSpace(); 
    Logger.debug("nameF " + name); 
    Logger.debug("sizeF " + totalSpace); 
    /* End debug*/ 

    try { 
     /* File user is not f file variable*/ 
     File fileUser = getFileUser(filename); 
     Try.of(fileUser::delete); 

     /* Use of org.apache.commons.io*/ 
     FileUtils.moveFile(f, getFileUser(filename)); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return ""; 
} 

private File getFileUser(String fileName) 
{ 
    return new File("public/upload/avatars", 
        user.getId().toString() + "." + FilenameUtils.getExtension(fileName)); 
} 

private boolean fileIsPicture(File f) 
{ 
    String mimetype = new MimetypesFileTypeMap().getContentType(f); 
    String type = mimetype.split("/")[0]; 
    return (type.equals("image")); 
} 

Ошибка:

java.io.FileNotFoundException: Source 'C:\Users\Alexis\AppData\Local\Temp\playtemp1188878075698565675\multipartBody8627595002998720963asTemporaryFile' does not exist 

Но я отладки с некоторыми информаций (почему 2 раза отладки?):

[info] play.api.Play - Application started (Dev) 
[info] application - avatar null 
[debug] application - nameFile KwizzyPicture.jpg 
[debug] application - contentFile image/jpeg 
[debug] application - nameF multipartBody1959853090277810547asTemporaryFile 
[debug] application - sizeF 763679993856 
[info] application - avatar null 
[debug] application - nameFile KwizzyPicture.jpg 
[debug] application - contentFile image/jpeg 
[debug] application - nameF multipartBody1959853090277810547asTemporaryFile 
[debug] application - sizeF 0 

Full code here

T приветствую вас за вашу помощь! Я французский, так жаль ошибок.

+0

Вставить полную трассировку стека, где FileNoutFoundException брошено. – Acapulco

+0

Хорошо, подождите несколько минут, я возвращаюсь –

+0

https://gist.github.com/Kwizzy-DEV/7c40ec62d7b371972e7c45f4690f7b7e –

ответ

0

вы должны использовать FileInputStream и FileOutptStream классы

Http.MultipartFormData.FilePart<File> file = body.getFile("avatar"); 

FileInputStream in = null; 
    FileOutputStream out = null; 
    try { 
    in = new FileInputStream(file); 
    //enter the file location in server 
    out = new FileOutputStream("output.jpeg"); 

    int c; 
    while ((c = in.read()) != -1) { 
     out.write(c); 
    } 
    }finally { 
    if (in != null) { 
     in.close(); 
    } 
    if (out != null) { 
     out.close(); 
    } 
    } 
+0

Я использую Spring FileCopy Util;) Но я думаю, что ваш anwser работа тоже –

Смежные вопросы