首页 > 解决方案 > 经常误用:Java 和 JSP 文件中的文件上传

问题描述

我在下面的几行中得到“经常被误用:文件上传”。任何人都可以建议修复。

  1. JAVA 文件:

    **public void setAttachedFile(FormFile formFile) { // File upload error at this line**
           attachedFile = formFile;
    
           if (attachedFile != null) {
                formData.put("attachedFile", attachedFile);
           } else {
                  formData.remove("attachedFile");
            }
    
          }
    
  2. JSP 文件:

    <table width="100%" border="0" cellspacing="0" cellpadding="3">
                <tr>
                  <td class="label" width="1%">&nbsp;</td>
                  <td class="label" width="22%">Select File Name</td>
                     <td class="field" width="26%">
                 **<input class="textfield width450" type="file" name="File_Name" maxlength="255" value=""> // Getting                  the fortify here**
                     </td>               
                   </tr>  
               </table>
    

谁能建议修复/解决方案

标签: javajspfile-uploadfortify

解决方案


您的后端代码是否验证文件的扩展名?

如果您的后端代码必须检查并验证文件的扩展名,那么您可以毫无问题地进行扫描。

C# 中的示例如下:

string extension = Path.GetExtension(file.FileName);
            
if (extension.ToLower().Trim() != ".xlsx" && extension.ToLower().Trim() != ".xls")
{
throw new Exception("Check the extension!");
}

和 JSP 文件:

<input class="textfield width450" 
type="file" name="File_Name" 
maxlength="255" value="" 
accept=".xlsx,.xls">

推荐阅读