2016-08-13 4 views
0
$uploads_dir = '/uploads'; 
foreach ($_FILES["pictures"]["error"] as $key => $error) { 
    if ($error == UPLOAD_ERR_OK) { 
     $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; 
     // basename() may prevent filesystem traversal attacks; 
     // further validation/sanitation of the filename may be appropriate 
     $name = basename($_FILES["pictures"]["name"][$key]); 
     move_uploaded_file($tmp_name, "$uploads_dir/$name"); 
    } 
} 

이것은 PHP를 사용하여 이미지를 업로드하는 방법입니다. JAVA와 동일한 기능이 있습니다. 나는 이미지를 업로드하고 JAVA를 사용하지만 폴더에 저장하고 싶다.JAVA를 사용하여 이미지 업로드 및 폴더 저장

조치는 양식 제출시 발생해야합니다. 모든 파일을 복사 한 폴더에서 다른 : 이 업로드 없음 서블릿은

+0

피드백 주시면 감사하겠습니다 – c0der

답변

0

이 도울 수

/** 
* Copy files from one directory to another. 
* @param sourceFile 
* @param destFile 
* @throws IOException 
*/ 
public static void copyAllDirFiles(File fromDir, File toDir) 
              throws IOException { 
    //check if source is a directory 
    if (!fromDir.isDirectory()) { 
     throw new IOException(fromDir + " directory not found."); 
    } 
    //if destination does not exit, create it 
    if (!toDir.exists()) { 
     toDir.mkdir(); 
    } 
    //get files from source 
    File[] files = fromDir.listFiles(); 
    for (File file : files) { 
     InputStream in = new FileInputStream(file); 
     OutputStream out = new FileOutputStream(toDir + "/" 
       + file.getName()); 
     // Copy the bits from input stream to output stream 
     byte[] buf = new byte[1024]; 
     int len; 
     while ((len = in.read(buf)) > 0) { 
      out.write(buf, 0, len); 
     } 
     in.close(); 
     out.close(); 
    } 
} 
관련 문제