2016-11-18 70 views
0

我想将.pdf文件和.jpg文件移动到特定文件夹,然后将特定位置路径保存到数据库。到目前为止,在谷歌的帮助下,我可以将文件复制到新位置(不移动),并将新路径保存到数据库,如下面的代码集所示。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
  • 移动()文件到目的地
  • 的open()与默认程序

根据添加在您的评论的要求:

  • 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

    @亨利我在答案中添加了新的文件名生成方法。 –

    相关问题