【问题标题】:How to create file on linux server using java如何使用java在linux服务器上创建文件
【发布时间】:2018-12-24 11:15:30
【问题描述】:

目前我将服务器从 Windows 转移到 linux,我的 java web 服务遇到了一些问题。我正在 Windows D 驱动器中创建一个文件。现在我也想在 linux 服务器上创建它。但我不知道如何给出路径以及如何创建(因为它没有驱动器作为 Windows)。所以需要一些帮助。我在下面发布我的 java 代码。

private static void receiveImg(String pic_bitmap) {
    FileOutputStream fos;
    try {

        fos = new FileOutputStream("D:\\AllImages\\ProfilePic\\Test.png");
        byte byteArray[] = Base64.decodeBase64(pic_bitmap);
        fos.write(byteArray);
        fos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

感谢您的帮助。

【问题讨论】:

标签: java linux file


【解决方案1】:

这应该可行。更多注意事项见下文

private static void receiveImg(String pic_bitmap) {
    FileOutputStream fos;
    try {

        fos = new FileOutputStream("/tmp/test.png");
        byte byteArray[] = Base64.decodeBase64(pic_bitmap);
        fos.write(byteArray);
        fos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

注意,你需要知道linux服务器的结构,才能把它保存在更合适的地方。如果您可以 ssh 到服务器上,您可以运行“ls /”命令来查看根文件夹结构。如果您不自己部署代码,则可能需要 DevOps 人员来帮助您了解部署代码的目标服务器上的文件夹结构。

【讨论】:

【解决方案2】:

这可以帮助你。它不会保存图像,但这无关紧要。

public void saveImage(String filename) { // filename ( path: linux ): /directory/filename 
    if(filename!=null){
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
            oos.writeObject('objectToWrite');
            oos.flush();
            oos.close();
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }           
    }else{
        System.out.println("NullPointerException");
    }
}

【讨论】:

    猜你喜欢
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 2012-01-19
    • 2020-03-09
    相关资源
    最近更新 更多