请查看我的代码:
package controller;
import java.io.File;
import java.io.Io Exception;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.dbLayer;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
*
* @author sarir
/*i am having jsp page with multipart enctype and saving image path in db
i am able to insert a first record successfully.Everything goes wellBut when a click a hyperlink again to insert second record and i`enter code here`nserting all the fields into the forma and submitting as posting a request to above servlet it does not produce output and neither any exceptions...any help would be appreciated..plz i am doing final year project...help me
*/
*/
public class multipartServlet extends HttpServlet {
//reading input from jsp page
String path="";
Long CRN;
String username="";
private ArrayList<String>list=new ArrayList<String>();
//to process enctype dtat
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, FileUploadException, Exception {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
username=request.getSession(true).getAttribute("uname").toString();
try {
CRN=generatorCRN.getCRN();
list.add(CRN.toString());
list.add(username);
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
Iterator<FileItem> iterator = upload.parseRequest(request).iterator();
File uploadedFile;
String dirPath="C:\\Users\\Bhatjaved\\Documents\\NetBeansProjects\\6thsem\\build\\web\\view\\Photo";
while (iterator.hasNext()) {
FileItem item = iterator.next();
if (!item.isFormField()) {
String fileNameWithExt = item.getName();
File filePath = new File(dirPath);
if (!filePath.exists()) {
filePath.mkdirs();
}
uploadedFile = new File(dirPath + "/"+CRN.toString().concat(".jpg"));
path=dirPath +filePath.separator+CRN.toString().concat(".jpg");
item.write(uploadedFile);
}
else {
String otherFieldName = item.getFieldName();
String otherFieldValue = item.getString();
if(otherFieldValue.equals("Select"))
;
else
list.add(otherFieldValue);
}
}
dbLayer dl=new dbLayer();
int res= dl.registerperson("missing_persons", list,path);
out.println(res);
if(res>0)
{
//request.getRequestDispatcher("/view/missingsucess.jsp").forward(request, response);
response.sendRedirect("/online_complaint/view/missingsucess.jsp?crn="+CRN);
}
}catch(Exception ex)
{
out.print(ex.getCause().toString());
}
finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (FileUploadException ex) {
Logger.getLogger(multipartServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(multipartServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (FileUploadException ex) {
Logger.getLogger(multipartServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(multipartServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
</editor-fold>
}
答案 0 :(得分:0)
从你的帖子中不清楚你第二次无法生成,但是一个明显的问题是list
。
您每次都无法清空此列表,并且根据您将其传递给(registerperson
)的方法的名称来判断,您可能会给它一个过度填充的对象。
也许这应该是你方法的本地方法,因为它不需要成为一个实例变量。