Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
玩2使用heroku上的Java,将文件临时保存_Java_Heroku_Playframework 2.0 - Fatal编程技术网

玩2使用heroku上的Java,将文件临时保存

玩2使用heroku上的Java,将文件临时保存,java,heroku,playframework-2.0,Java,Heroku,Playframework 2.0,一开始,我为我的英语感到抱歉 我正在用java开发一个play应用程序,并将其部署到heroku。 我喜欢创建一张图片(确切地说是QRCode),将其临时存储并显示在下一页上。 我确实知道herokus的临时文件系统,但如果我理解正确,在cedar堆栈上,我可以在任何我喜欢的地方创建文件,只要它是可以的,它们不会被存储很长时间。该应用程序只需要生成一个QR,我扫描它,文件可能会被删除 似乎没有创建该文件。关于如何临时保存和显示QRs,有什么想法吗 控制器 public class Applica

一开始,我为我的英语感到抱歉

我正在用java开发一个play应用程序,并将其部署到heroku。 我喜欢创建一张图片(确切地说是QRCode),将其临时存储并显示在下一页上。 我确实知道herokus的临时文件系统,但如果我理解正确,在cedar堆栈上,我可以在任何我喜欢的地方创建文件,只要它是可以的,它们不会被存储很长时间。该应用程序只需要生成一个QR,我扫描它,文件可能会被删除

似乎没有创建该文件。关于如何临时保存和显示QRs,有什么想法吗

控制器

public class Application extends Controller {

    private static String workingDirectory = "public/images/";
    public static Result qrCode() {
        String msg = "I am a QR-String";
        BufferedImage image = (BufferedImage) QR.stringToImage(msg);

        String imgPath = workingDirectory+"posQR.png";

        try{
            File outputfile = new File(imgPath);
            ImageIO.write(image,"png",outputfile);
        }catch(IOException e){
            e.printStackTrace();
        }
        return ok(views.html.qrCode.render());
    }
}
public class Application extends Controller {

    private static String workingDirectory = "public/images/";
    public static Result qrCode() {
        String msg = "I am a QR-String";
        BufferedImage image = (BufferedImage) QR.stringToImage(msg);
    File outputfile = null;
        String imgPath = workingDirectory+"posQR.png";

        try{
            outputfile = File.createTempFile("posQR",".png");
            ImageIO.write(image,"png",outputfile);
        }catch(IOException e){
            e.printStackTrace();
        }
            return ok(views.html.qrCode.render(outputfile.getAbsolutePath()));
}
查看qrCode

<img src="@routes.Assets.at("images/posQR.png")">
@(qrPath: String)
...
<img id="qr" src=@qrPath>
@(qrPath: String)
...
<img src="@routes.Application.getImage(qrPath)"/>
查看qrCode

<img src="@routes.Assets.at("images/posQR.png")">
@(qrPath: String)
...
<img id="qr" src=@qrPath>
@(qrPath: String)
...
<img src="@routes.Application.getImage(qrPath)"/>
@(qrPath:String)
...

工作目录是否以文件分隔符结尾

String imgPath = workingDirectory+"posQR.png";
这帮助了我:

最后我做到了。诀窍不是执行src=path,而是执行src=getImage(path)。仍然很奇怪,但现在它工作了

路线

GET /tmp/*filepath  controllers.Application.getImage(filepath: String)
应用程序

public class Application extends Controller {

public static Result qrCode() {
    String msg = "I am a QR-String";
    BufferedImage image = (BufferedImage) QR.stringToImage(msg);
    File outputfile = null;

    try{
        outputfile = File.createTempFile("posQR",".png");
        ImageIO.write(image,"png",outputfile);
    }catch(IOException e){
        e.printStackTrace();
    }
    return ok(views.html.qrCode.render(outputfile.getAbsolutePath()));
}
...

public static Result getImage(String imgPath){
    return ok(new File(imgPath));
}
}
查看qrCode

<img src="@routes.Assets.at("images/posQR.png")">
@(qrPath: String)
...
<img id="qr" src=@qrPath>
@(qrPath: String)
...
<img src="@routes.Application.getImage(qrPath)"/>
@(qrPath:String)
...

感谢您的帮助:D

您在写入磁盘时是否得到堆栈跟踪?它在本地上运行得很好,但在heroku上运行得不好。或者在Play 2.x中可能有一个与此等效的程序?是的,它是私有静态字符串workingDirectory=“public/images/”;方法写入应返回布尔值作为结果。这是怎么一回事?顺便说一句,还可以尝试outputfile.getAbsolutePath()和outputfile.canWrite()。另一种可能是File.createTempFile()。write的返回值为true,canWrite()也是。无论是本地还是heroku。两个绝对路径都以/public/images/posQR.pngTry File.createTempFile()结尾,而不是手动放置文件。此方法必须有效,否则会出现完全错误。TempFile已创建。我怎样才能在temp目录中访问这个丑陋的数字部分呢?