无法打开我从服务器上传的文件?

发布于 2024-12-29 10:50:03 字数 2159 浏览 0 评论 0原文

我在 Tomcat 服务器上托管一个网站。该应用程序使用 Struts 1.1 和 Spring 进行所有操作。我有一个用于将文件上传到服务器的页面。 当用户上传任何文件时,它会成功上传,但在尝试检索时会出现 404 错误。我使用 SSH 登录检查了文件,上传的文件位于该位置。过去 4 天我一直在为这个问题摸不着头脑,但没有解决方案。它在我的本地机器上正常工作,没有任何问题。问题出在部署中。

重要提示:通过 SSH 登录,如果我尝试将该文件移动到其他位置,然后将其放回原来的位置,我就能够检索该文件..!!!我不知道为什么,但我无法对用户上传的每个文件执行此操作。因此,我修改了代码,以便首先将文件上传到临时位置,然后将其移动到正确的位置。但即使这样也行不通。

FileOutputStream outputStream = null;
FormFile formFile = null;
String tempFilePath = getServlet().getServletContext()
        .getRealPath("/")
        + "uploads"
        + System.getProperty("file.separator") + "temp";
 try
 {
    formFile = uploadForm.getFile();

boolean errorflag = false;
if(formFile.getFileSize() > 10660000)
{
    request.setAttribute("error",
            "File size cannot exceed 10MB!");
    errorflag = true;
}
else
{
    errorflag = validateFileUpload(request,
            formFile, errorflag);
}
if(errorflag)
{
    return gotoKnowledgeSharingPage(mapping,
            request, actionHelper, session, userid,
            instid);
}

File folder = new File(tempFilePath);
if(!folder.exists())
{
    folder.mkdir();
}
outputStream = new FileOutputStream(new File(
        tempFilePath, formFile.getFileName()));
outputStream.write(formFile.getFileData());
}   
finally
{
    if(outputStream != null)
    {
        outputStream.flush();
        outputStream.close();
    }
}    

String finalFilePath = getServlet().getServletContext()
        .getRealPath("/")
        + "uploads"
        + System.getProperty("file.separator")
        + session.getAttribute("userid");
//+ System.getProperty("file.separator")
// + formFile.getFileName();

File oldPath = new File(tempFilePath
        + System.getProperty("file.separator")
        + formFile.getFileName());
// Move file to new directory  
File newPath = new File(finalFilePath);
if(!newPath.exists())
{
newPath.mkdir();
} 
boolean success = oldPath.renameTo(new File(
    finalFilePath, formFile.getFileName()));
if(success)
{
    actionHelper.insertIntoUploadTable(userid,
            knowledgeForm, formFile.getFileName()); 
}
else
{
    if(oldPath.exists())
    {
        oldPath.delete();
    }
}

I am hosting a website on Tomcat server. The application uses Struts 1.1 and Spring for all its operations. I have a page that is used for uploading files to the server.
When user uploads any file it is successfully uploaded but gives a 404 error when tried to retrieve. I checked the file using SSH login, the uploaded file is present in that location. I am scratching my head over this problem from past 4 days but no solution. Its works properly without any problems in my local machine. The problem in there in the deployment.

An Important note: From SSH login, If i try to move that file to some other location and then place it back to its original location, i am able to retrieve the file..!!! I don't know why but I can't do this for every file uploaded by the user. So i modified the my code so that the file is uploaded to a temp location first and then moving it to the correct location. But even this is not working.

FileOutputStream outputStream = null;
FormFile formFile = null;
String tempFilePath = getServlet().getServletContext()
        .getRealPath("/")
        + "uploads"
        + System.getProperty("file.separator") + "temp";
 try
 {
    formFile = uploadForm.getFile();

boolean errorflag = false;
if(formFile.getFileSize() > 10660000)
{
    request.setAttribute("error",
            "File size cannot exceed 10MB!");
    errorflag = true;
}
else
{
    errorflag = validateFileUpload(request,
            formFile, errorflag);
}
if(errorflag)
{
    return gotoKnowledgeSharingPage(mapping,
            request, actionHelper, session, userid,
            instid);
}

File folder = new File(tempFilePath);
if(!folder.exists())
{
    folder.mkdir();
}
outputStream = new FileOutputStream(new File(
        tempFilePath, formFile.getFileName()));
outputStream.write(formFile.getFileData());
}   
finally
{
    if(outputStream != null)
    {
        outputStream.flush();
        outputStream.close();
    }
}    

String finalFilePath = getServlet().getServletContext()
        .getRealPath("/")
        + "uploads"
        + System.getProperty("file.separator")
        + session.getAttribute("userid");
//+ System.getProperty("file.separator")
// + formFile.getFileName();

File oldPath = new File(tempFilePath
        + System.getProperty("file.separator")
        + formFile.getFileName());
// Move file to new directory  
File newPath = new File(finalFilePath);
if(!newPath.exists())
{
newPath.mkdir();
} 
boolean success = oldPath.renameTo(new File(
    finalFilePath, formFile.getFileName()));
if(success)
{
    actionHelper.insertIntoUploadTable(userid,
            knowledgeForm, formFile.getFileName()); 
}
else
{
    if(oldPath.exists())
    {
        oldPath.delete();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文