2016-08-13 17 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(); 
    } 
} 
関連する問題