上传文件到Glassfish备用文档根文件夹

Uploading file to Glassfish alternate document root folder

本文关键字:备用 文档 根文件夹 Glassfish 文件      更新时间:2023-09-26

我在glassfish-web.xml中为Glassfish创建了一个备用文档根:

<glassfish-web-app>
    <property name="alternatedocroot_1" value="from=/Database/* dir=c:/GetSome" />
</glassfish-web-app>

JS上传文件,如果成功,则加载jsp和上传的文件:

function Changed_inputFileLogo()
{
    /*console.log("function Changed_inputFileLogo");*/
    current = event.target;
    var data = new FormData();
    $.each(current.files, function(key, value)
    {
        data.append(key, value);
    });
    data.append("idOrganization", _Organization.id);
    data.append("requestPage", true);
            $.ajax({
            type: 'POST',
            async: false,
            url: 'UploadLogo',
            data: data,
            processData: false,
            contentType: false,
            success: function(response)
            {
                _Organization.logo = 1;
                $.ajax({
                type: 'POST',
                async: false,
                url: 'PhotoExist.jsp',
                data: "path=" + "Database/LogoOrganization/"+_Organization.id+".jpg",
                success: function(response)
                {
                    $("#divLogo").html(response);
                },
                error: function(xhr, textStatus, errorThrown) 
                {
                    LogConsole_ajaxError(xhr, textStatus, errorThrown);
                }
                });
            },
            error: function(xhr, textStatus, errorThrown) 
            {
                LogConsole_ajaxError(xhr, textStatus, errorThrown);
            }
        });
}

这是PhotoExist.jsp文件:

...
<%  String path  = request.getParameter("path");%>
...
        <div class="divPhotoWrapper HavePhoto" onmouseover="ShowPhotoButton()" onmouseout="HidePhotoButton()">
            <div class="divDeletePhoto displayNone">
                <button class="buttonDeletePhoto" onclick="DeletePhoto()"></button>
            </div>
            <a id="aLogo">
                <img id="imgLogo" src="<%=path + "?time=" +System.currentTimeMillis()%>" width="100" height="100">
            </a>
        </div>
...

这是servlet,它将上传的文件复制到备用文档根文件夹:

...
try
{
    filePart = request.getPart("0"); 
    String fullName = Common._FolderDatabase_LogoOrganization + File.separator + idOrganization + ".jpg";
    File FileTemp = new File(fullName);
    out = new FileOutputStream(FileTemp);
    filecontent = filePart.getInputStream();
    int read = 0;
    final byte[] bytes = new byte[1024];
    while ((read = filecontent.read(bytes)) != -1) 
    {
        out.write(bytes, 0, read);
    }
}
catch(IOException exception)
{
    Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Unable to copy file." + " Exception: ", exception);
    response.sendError(HttpServletResponse.SC_ACCEPTED, "Unable to copy file.");
    return;
}
finally 
{
    if (out != null) 
        out.close();
    if (filecontent != null) 
        filecontent.close();
}
...

它工作良好,但有时(十分之一的尝试)浏览器无法下载图像源,并给出这个错误:GET http://localhost:8080/GetSome/Database/LogoOrganization/85.jpg?time=1407933088377 404 (Not Found)。我尝试在servlet中检查文件是否可用,如下所示:

File FileTemp = new File(fullName);
while(!FileTemp.canWrite())
{
    Thread.sleep(1000);
}

,像这样:

in = new FileInputStream(FileTemp);
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = in.read(bytes)) != -1) 
{
}

在这两种情况下,文件都是可用的,但它没有帮助。我猜Glassfish上传到文件夹后没有时间访问文件

我通过检查Glassfish资源的可用性来解决这个问题

URL url = getServletContext().getResource(requestUri);
if (url != null) { 
    try
    {
        Thread.sleep(100);
    } 
    catch (InterruptedException ex)
    {
        Logger.getLogger(UploadLogo.class.getName()).log(Level.SEVERE, null, ex);
    }
}

感谢你的回答