Java EE-@MultipartConfig不起作用

时间:2018-07-18 22:40:41

标签: java java-ee java-ee-7

我有一个.jsp文件,该文件通过表单将multipart/form-data发送到servlet,并且当我将请求传递到servlet(从action属性)时,我尝试获取request.getParts()和得到Unable to process parts as no multi-part configuration has been provided错误。请注意,启动.jsp文件后,即使尝试通过表单上传数据,我也会收到相同的消息。因此问题可能出在.jsp文件中。我在Servlet上使用了@MultipartConfig,但没有用。

这是我的DisplayAlbum.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<title>Photo Album</title>
<head></head>
<jsp:useBean id="photoHandleBean" class="src.PhotoHandleBean"/>
<body>
    <div>
        <form method="post" action="/RequestServlet" enctype="multipart/form-data">
            <input type="file" name="image">
            <input type="text" name="txt">
            <input type="submit" value="Update">
        </form>
    </div>
</body>
</html>

RequestServlet类

package src;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet(name="RequestServlet" ,urlPatterns="/RequestServlet")
@MultipartConfig
public class RequsetServlet extends HttpServlet{

    @Override
    public void doGet(HttpServletRequest rq, HttpServletResponse rs) throws ServletException, IOException{

        PhotoAlbum pa = PhotoAlbum.getAlbum(rq.getSession());
        if(rq.getContentType() != null && rq.getContentType().contains("multipart/form-dasta")) {
            System.out.println("osdjfosdj");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            InputStream input = null;
            String filename = "";
            for(Part p: rq.getParts()) {
                input = p.getInputStream();
                int c = 0;
                while((c = input.read()) != -1) {
                    baos.write(c);
                }
                filename = p.getSubmittedFileName();
            }
            input.close();
            baos.close();
            System.out.println(baos.toByteArray()+"<<<");
            pa.addPhoto(baos.toByteArray(), filename);

        }

        RequestDispatcher rd = getServletContext().getRequestDispatcher("/DisplayAlbum.jsp");
        rd.forward(rq, rs);
    }

    @Override
    public void doPost(HttpServletRequest rq, HttpServletResponse rs) throws ServletException, IOException{
        doGet(rq, rs);
    }

}

和PhotoAlbum类

package src;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateful;
import javax.servlet.http.HttpSession;

public class PhotoAlbum {
    private List<String> filename = new ArrayList<String>();
    private List<byte[]> imageFile = new ArrayList<byte[]>();

    public static PhotoAlbum getAlbum(HttpSession session) {
        if(session.getAttribute("album") == null) {
            PhotoAlbum pa = new PhotoAlbum();
            session.setAttribute("album", pa);
        }
        return (PhotoAlbum) session.getAttribute("album");
    }

    public List<byte[]> getImages(){
        return this.imageFile;
    }

    public int getCount() {
        return imageFile.size();
    }

    public void addPhoto(byte[] photoByte, String filename) {
        imageFile.add(photoByte);
        this.filename.add(filename);
    }
}

0 个答案:

没有答案