Reputation: 1097
so in my class i was ordered to upload images to database and get them out and show them on a website, pretty simple when using servlet or php, but i was asked to do it using only JSP files. to do so i had to get an image from the user and pass it as a parameter and get a part of the parameter, not a problem.
the problem started when i tried to run it and the server asked for @multipartconfig annotation. i cant find a way to add it to the jsp code.
<%@page import="javax.servlet.annotation.MultipartConfig"%>
<%@page import="java.sql.*"%>
<%@page import="java.io.InputStream"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
Part p = request.getPart("image");
InputStream inputStream = null;
if (p != null) {
inputStream = p.getInputStream();
}
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test42";
Class.forName(driver);
Connection con = DriverManager.getConnection(url, "root", "1234");
String sqlString = "INSERT INTO test42.images items(idimages) values(" + inputStream + ");";
String msg = p.toString();
%>
<form method="post" action="mainPage.jsp" enctype="multipart/form-data">
choose file :
<input type="file" name="image" />
<input type="submit" value="submit">
</form>
and this is the messege from the server:
java.lang.IllegalStateException: Request.getPart is called without multipart configuration. Either add a @MultipartConfig to the servlet, or a multipart-config element to web.xml
i tried adding it to the web.xml but it didnt work.... the app just crash with a build error; i got this solution from here: http://docs.oracle.com/javaee/6/tutorial/doc/gmhal.html
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<multipart-config>
<location>/tmp</location>
<max-file-size>20848820</max-file-size>
<max-request-size>418018841</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
</web-app>
Upvotes: 1
Views: 9325
Reputation: 4474
The <multipart-config>
tag needs to go inside the <servlet>
tag and you need to have a <servlet-mapping>
along the <servlet>
tag.
<servlet>
<servlet-name>uploadfile</servlet-name>
<jsp-file>/mainPage.jsp</jsp-file>
<multipart-config>
<location>/temp</location>
<max-file-size>20848820</max-file-size>
<max-request-size>418018841</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>uploadfile</servlet-name>
<url-pattern>/mainPage.jsp</url-pattern>
</servlet-mapping>
Upvotes: 6
Reputation: 1
Actually every jsp file will be converted to servlet because tomcat can only address servlet and so every jsp file is indirectly a servlet and has all the features of it
Upvotes: 0