1

I am uploading a image using jsp and servlet. I am using enctype=multipart/form-data. But i can not fetch input field values into servlet.

Here is my jsp code

<form method="post" action="uploadImage" enctype="multipart/form-data">
        <p>Select file to upload:</p>
        <input type="file" name="file" >
        <input type="text" name="name" >
        <br><br>
        <input type="submit" value="Upload" >
    </form>

And here is my sevlet code

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
           response.setContentType("text/html;charset=UTF-8");
    HttpSession sess=request.getSession();
    PrintWriter out = response.getWriter();
int i=0;
    String u_id=(String) sess.getAttribute("uid");
    String saveFile="";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}

saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));

saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));

String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;

File ff = new File("D:/images/"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
try{
Connection con;
PreparedStatement ps,ps1;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dfmc","root","93Pb3gaNv0");
String sql="insert into profilepic_table(uid, image_name) values('"+u_id+"','"+ff.getName()+"')";
ps=con.prepareStatement(sql);
ps.executeUpdate();
}
}catch(Exception e){
e.printStackTrace();
}
}

Also i want to select multiple files and upload them.

nadeem ahmad
  • 181
  • 1
  • 1
  • 10
  • Please add roseindia.net to your Internet blacklist. Never never never use that side. It'll only lead you into a downward spiral of bad code and bad practices. – BalusC May 06 '15 at 08:47

1 Answers1

0

Add below lines of code before you define your Servlet, to handle multipart/form-data submissions you have to use Multipart annotation

@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 2, // 2MB
    maxFileSize = 1024 * 1024 * 10, // 10MB
    maxRequestSize = 1024 * 1024 * 50)

public class ServletName extends HttpServlet { ... }

Also, import javax.servlet.annotation.MultipartConfig;

Deepika Rajani
  • 564
  • 5
  • 15
  • not working bro...error! java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: class MultipartConfig – nadeem ahmad Apr 14 '15 at 06:55
  • Try after adding `@WebServlet("/ServletName")` and import `javax.servlet.annotation.WebServlet;` – Deepika Rajani Apr 14 '15 at 07:12
  • After that this error is showing ...java.lang.StringIndexOutOfBoundsException: String index out of range: -1 – nadeem ahmad Apr 14 '15 at 07:22
  • java.lang.StringIndexOutOfBoundsException: String index out of range: -1 java.lang.String.substring(String.java:1954) uploadImage2.processRequest(uploadImage2.java:86) uploadImage2.doPost(uploadImage2.java:255) javax.servlet.http.HttpServlet.service(HttpServlet.java:644) javax.servlet.http.HttpServlet.service(HttpServlet.java:725) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) – nadeem ahmad Apr 14 '15 at 09:06
  • 1
    Debug your code and check does variable **file** has some value. It is either of the line which is throwing error. `saveFile = file.substring(file.indexOf("filename=\"") + 10);` `saveFile = saveFile.substring(0, saveFile.indexOf("\n"));` `saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));` – Deepika Rajani Apr 14 '15 at 11:34
  • can u give me ur email id..... – nadeem ahmad Apr 15 '15 at 08:58