JSP相当于ASP Request.Files

时间:2013-06-16 01:03:28

标签: jsp file-upload

这就是我在ASP中所做的:

在表格上:

<form id="form1" runat="server">
   ...
   <asp:Button ID="cmdGetFileToUpload" runat="server" Text="Send File" />
   ...
   <asp:Panel id=divUploadFile Visible="False" ...>
      <input id="inpUploadFile" runat="server" name="inpUploadFile" type="file" />
      <asp:Button ID="cmdUploadFile" runat="server" Text="Upload File" />
   </asp:Panel
   ...
</form>

在背后的代码中:

Protected Sub cmdGetFileToUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetFileToUpload.Click
   'This shows the upload file panel when the user clicks the Send Files button
   Me.divUploadFile.Visible = True

   ... hide other panels
End Sub

Protected Sub cmdUploadFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUploadFile.Click
   Dim oFile As HttpPostedFile
   Dim sFullPathName as String
   Dim sFileName as String

   oFile = Request.Files(0)
   If oFile.FileName <> "" Then
      sFullPathName = oFile.FileName
      sFileName = sFullPathName.Substring(sFullPathName.LastIndexOf("\") + 1)
      oFile.SaveAs(Server.MapPath(".\myFolder\") & sFileName)
   End If

   ... do stuff with the file
End Sub

我已经下载了commons-fileupload.jar及其依赖项,并将它们放在我的项目中。但我不清楚代码的放置位置。我在回发后设置我的页面时调用了一个类方法,所以把它放在那里,但servletfileupload.ismultipartcontent(request)总是返回false。文档说通常是因为请求已经处理完毕。所以,我将代码移到了表单的顶部,如下所示:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"
    import="myClasses.myClass,
        org.apache.commons.fileupload.*,
        org.apache.commons.fileupload.servlet.*,
        org.apache.commons.fileupload.disk.*,
        org.apache.commons.fileupload.servlet.*,
        java.io.*, java.util.*"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>MyPageTile</title>
    <style>
        ... various styles
    </style>
    <script type="text/javascript">
        ... various scripts
    </script>
</head>
<body style="background-color:tan;" onload="window.history.forward();" >
    <%@ include file="subforms/my_banner.jsp" %>

<%  if (session.getAttribute("user")==null) { response.sendRedirect("index.jsp"); } 
    else {
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (isMultipart) {
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletContext servletContext = this.getServletConfig().getServletContext();
            File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
            factory.setRepository(repository);
            ServletFileUpload upload = new ServletFileUpload(factory);
            List<FileItem> items = upload.parseRequest(request);
            Iterator<FileItem> iter = items.iterator();
            while (iter.hasNext()) {
                FileItem item = iter.next();
                if ( ! item.isFormField() ) {
                    File uploadedFile = new File( "/uploads/" + session.getAttribute("user") + ".xlsm");
                    item.write(uploadedFile);
                }
            }
        }
... other stuff then my form
<form id="AdminForm" enctype="multipart/form-data">

但是,servletfileupload.ismultipartcontent(request)再次返回false。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我发现了这个问题 - 我的表单标记没有将方法 attibute设置为发布。我还有调试要做,但我至少已经过了这个障碍了。希望这对其他人有用。