2016-11-18 9 views
0

.pdfファイルと.jpgファイルを特定のフォルダに移動した後、特定の場所パスをデータベースに保存します。これまでのところGoogleの助けを借りて、ファイルを新しい場所にコピーし(移動しないで)、下のコードセットに示すように新しいパスをデータベースに保存することができました。私が知りたいのは何JAVA .JPGファイルと.PDFファイルを特定の場所に移動する

try { 
    JFileChooser choose = new JFileChooser(); 
    choose.showOpenDialog(null); 
    File f = choose.getSelectedFile(); 
    File sourceFile = new File(f.getAbsolutePath()); 
    File destinationFile = new File("D:\\" + sourceFile.getName()); 

    FileInputStream fileInputStream = new FileInputStream(sourceFile); 
    FileOutputStream fileOutputStream = new FileOutputStream(destinationFile); 

    int bufferSize; 
    byte[] bufffer = new byte[512]; 
    while ((bufferSize = fileInputStream.read(bufffer)) > 0) { 
     fileOutputStream.write(bufffer, 0, bufferSize); 
     } 

    fileInputStream.close(); 
    fileOutputStream.close(); 

} 
catch (Exception e){ 
    e.printStackTrace(); 
    } 

は、ユニークな名前の代わりに、コピーしたファイルを移動する方法

  1. ですか..?
  2. そのファイルが正常に移動されたかどうかを表示するには またはJOptionpaneにはない(その場合、私は の部分を挿入できます)..?これらの画像を取得する方法を
  3. は( 'を報告開くためにここにクリック 『のような)を直接開くには、それはコンピュータのデフォルト 画像ビューアやPDFビューア

に開いている必要がありますリンク私は』助けてくださいグーグルで2週間半疲れています。おかげでここでは誰も

答えて

0

は何をしたい、あなたが行うことができる方法の例です:

  • は(選択)PDFやJPG
  • 移動()(オープン宛先へ
  • ファイル)とに限定デフォルトのプログラム

あなたのコメントで要求を応じて追加しました:

  • getFileExtension()
  • generateDestinationPath(最後のドットの後の文字列を取得する)は、今日のタイムスタンプ+インデックス+拡張子から

編集したクラス:

package testingThings; 

    import java.awt.Desktop; 
    import java.io.IOException; 
    import java.nio.file.Files; 
    import java.nio.file.Path; 
    import java.nio.file.Paths; 
    import java.text.SimpleDateFormat; 
    import java.util.Arrays; 
    import java.util.Calendar; 
    import java.util.Date; 

    import javax.swing.JFileChooser; 
    import javax.swing.JOptionPane; 
    import javax.swing.filechooser.FileNameExtensionFilter; 

    public class FileHandler { 

     public Path choose() { 
      JFileChooser choose = new JFileChooser(); 
      choose.setFileFilter(new FileNameExtensionFilter("PDF and JPG", "pdf", "jpg")); 
      choose.showOpenDialog(null); 

      Path sourcePath = choose.getSelectedFile().toPath(); 

      return sourcePath; 
     } 

     public void move(Path sourcePath, Path destinationPath) { 
      try { 
       Files.move(
         sourcePath, 
         destinationPath//, 
         // since the destinationPath is unique, do not replace 
    //     StandardCopyOption.REPLACE_EXISTING, 
         // works for moving file on the same drive 
         //its basically a renaming of path 
    //     StandardCopyOption.ATOMIC_MOVE 
       ); 
    //   JOptionPane.showMessageDialog(null, "file " + sourcePath.getFileName() + " moved"); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       JOptionPane.showMessageDialog(
         null, 
         "moving failed for file: " + sourcePath.getFileName(), 
         "Error", 
         JOptionPane.ERROR_MESSAGE 
       ); 
       e.printStackTrace(); 
       System.exit(1); 
      } 
     } 

     public void open(Path destinationPath) { 
      try { 
       Desktop.getDesktop().open(destinationPath.toFile()); 
      } catch (IOException e1) { 
       JOptionPane.showMessageDialog(
         null, 
         "file openning fails: " + destinationPath.getFileName(), 
         "Error", 
         JOptionPane.ERROR_MESSAGE 
       ); 
       System.exit(1); 
      } 
     } 

     public static void main(String[] args) { 
      FileHandler fileHandler = new FileHandler(); 
      Path sourcePath = fileHandler.choose(); 

      String extension = fileHandler.getFileExtension(sourcePath); 
      Path destinationPath = fileHandler.generateDestinationPath(extension); 

      fileHandler.move(sourcePath, destinationPath); 
      fileHandler.open(destinationPath); 
     } 

     /** 
     * Generate a path for a file with given extension. 
     * The Path ist hardcoded to the folder "D:\\documents\\". The filename is the current date with appended index. For Example: 
     * <ul> 
     * <li>D:\\documents\\2016-11-19__12-13-43__0.pdf</li> 
     * <li>D:\\documents\\2016-11-19__12-13-43__1.pdf</li> 
     * <li>D:\\documents\\2016-11-19__12-13-45__0.jpg</li> 
     * </ul> 
     * @param extension 
     * @return 
     */ 
     public Path generateDestinationPath(String extension) { 
      Date today = Calendar.getInstance().getTime(); 
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd__HH-mm-ss"); 

      String filename; 
      Path destinationPath; 
      int index = 0; 

      do { 
       filename = sdf.format(today) + "__" + index + "." + extension; 
       destinationPath = Paths.get("D:\\documents\\" + filename); 
       destinationPath = Paths.get("C:\\Users\\ceo\\AppData\\Local\\Temp\\" + filename); 
       System.out.println(destinationPath); 
       index++; 
      } 
      while (destinationPath.toFile().exists()); 

      return destinationPath; 
     } 

     /** 
     * Return the String after the last dot 
     * @param path 
     * @return String 
     */ 
     public String getFileExtension(Path path) { 
      String[] parts = path.toString().split("\\."); 
      System.out.println(path); 
      System.out.println(Arrays.toString(parts)); 
      System.out.println(parts.length); 

      String extension = parts[parts.length - 1]; 
      return extension; 
     } 
    } 
+0

それに一意の名前を与えるためにどのように.. ? (img2016-09-13-55.jpgやimg2016-11-19-20-22.pdfのように)...? オペレータは、ドキュメントをアップロードするときに名前を変更する必要がないためです。もしオペレータがユニークな名前を付けずにアップロードしなかった場合は、既にデータベースにあるドキュメントを置き換えることになります。 – Henry

+0

ファイルの移動のためのコーディングが機能しなかったので、 'StandardCopyOption.ATOMIC_MOVE'機能を削除しました。その後、それは働いた。どうしてそういうことが起こっているのですか? 'java.nio.file.AtomicMoveNotSupportedException:' – Henry

+0

@Henry私は答えで新しいファイル名生成メソッドを追加しました。 –

関連する問題