2

Tomcat 7.0.27 で実行されている Web サイトがあり、次の Ajax JavaScript コードを使用して Web サイトに画像を動的にアップロードします。

var fileInput = document.getElementById("uploadfile");
var formData = new FormData();
formData.append("uploadfile", fileInput.files[0]);

$.ajax({
   url: '<c:url value="/something/PhotoUpload"/>',
   type: 'POST',
   data: formData,
   async: true,
   cache: false,
   dataType: 'json',
   processData: false, // Don't process the files
   contentType: false, // Set content type to false as jQuery will tell the server its a query string request
   success: function(data, textStatus, jqXHR)
   {
        // do something;    
   },
   error: function(jqXHR, textStatus, errorThrown)
   {
       // Handle errors here
   }
});

サーバー側は、次の post メソッドを持つサーブレットです。

@MultipartConfig
public class PhotoUploadAjaxServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Part part = request.getPart("uploadfile");
        if (part != null) {
             //Process image
        }
    }
}

これはすべてうまく機能し、「部分」にはアップロードされたファイルが含まれています。一部のセキュリティの脆弱性から保護するために HDIV ライブラリを追加したところ、"part" が突然 null になりました。理由がわかりません。

アプリケーションの残りの部分は Struts 1.2.7 を使用し、その部分は HDIV によって保護されていますが、/PhotoUpload の URL は保護されていないため、HDIV は要求にまったく触れないでください。ここに hdiv-config.xml があります:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:hdiv="http://www.hdiv.org/schema/hdiv"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.hdiv.org/schema/hdiv http://www.hdiv.org/schema/hdiv/hdiv.xsd">

<hdiv:config errorPage="/InternalError.do" excludedExtensions="css,png,gif,jpeg,jpg,js"
    protectedExtensions="/something/*.do, /something/*.action" maxPagesPerSession="20" confidentiality="true"
    avoidValidationInUrlsWithoutParams="true" reuseExistingPageInAjaxRequest="true">
    <hdiv:sessionExpired homePage="/" />
    <hdiv:startPages>/Login.do</hdiv:startPages>
    <hdiv:startPages>/LoginSubmit.do</hdiv:startPages>
    <hdiv:startPages>/InternalError.do</hdiv:startPages>
    <!-- Some things removed to protect the innocent -->
    <hdiv:startParameters>org.apache.struts.action.TOKEN,org.apache.struts.taglib.html.TOKEN</hdiv:startParameters>

</hdiv:config>

</beans>

私が気づいた奇妙なことは、アップロードされた画像が十分に大きい場合 (たとえば 3MB)、HDIV にもかかわらず、すべてが再び機能することです。「part」には、本来あるべきファイルが含まれています。ログには次のように表示されます。

 org.hdiv.config.multipart.StrutsMultipartConfig:79 - Size limit exceeded exception

したがって、マルチパート データがメモリに格納されている場合、HDIV の構成が何らかの形で間違っていると思います。データのサイズが十分に大きくなると、データはファイル システムに保存され、機能します。

私は何を間違っていますか?

4

1 に答える 1

0

そのファイルをカスタム サーブレットで処理する代わりに、Struts アクションでファイルを処理する必要があります。

ここで例を見ることができます。

于 2015-06-16T12:50:34.827 に答える