السلام عليكم ورحمة الله ..
يا إخوان انا عملت فلتر على الـ upload servlet .. بحيث لو عمل upload بدون قيمة، يرجعه لنفس الصفحة السابقة مع طباعة رسالة خطأ ..
لكن الخطأ عندي ان السيرفلت توقف عن العمل بعد الفلتر .. أرجو المساعدة في حل هذه المشكلة..
فلتر upload:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
HttpSession session = ((HttpServletRequest) request).getSession();
String url = filterConfig.getServletContext().getContextPath();
HttpServletRequest req = (HttpServletRequest) request;
String a = req.getParameter("file1");
if(req.getParameter("file1") != null){
chain.doFilter(request, response);
}
else{
HttpServletResponse res = (HttpServletResponse) response;
session.setAttribute("error", "ERROR!!");
res.sendRedirect(url+"/upForm.jsp");
}
}upload Servlet:
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
*
* @author Admin
*/
public class uploadServlet extends HttpServlet {
private static final String TMP_DIR_PATH = "c:\\tmp";
private File tmpDir;
private static final String DESTINATION_DIR_PATH ="/images";
private File destinationDir;
private String Path ="http://localhost:8084/wbtest/app/protected/Account/home.jsp";
private String action;
private final String addNewCtg = "ctg";
private final String addNewPro = "pro";
@Override
public void init(ServletConfig config) throws ServletException
{
super.init(config);
tmpDir = new File(TMP_DIR_PATH);
if(!tmpDir.isDirectory()) {
throw new ServletException(TMP_DIR_PATH + " is not a directory");
}
String realPath = "C:\\Users\\Admin\\Documents\\NetBeansProjects\\wbtest\\web\\"+DESTINATION_DIR_PATH;
destinationDir = new File(realPath);
if(!destinationDir.isDirectory()) {
throw new ServletException(DESTINATION_DIR_PATH+" is not a directory");
}
}
// <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
*/
/**
* 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 {
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory ();
/*
*Set the size threshold, above which content will be stored on disk.
*/
//fileItemFactory.setSizeThreshold(1*1024*1024); //1 MB
/*
* Set the temporary directory to store the uploaded files of size above threshold.
*/
fileItemFactory.setRepository(tmpDir);
ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
try {
/*
* Parse the request
*/
List items = uploadHandler.parseRequest(request);
Iterator itr = items.iterator();
while(itr.hasNext()) {
FileItem item = (FileItem) itr.next();
/*
* Handle Form Fields.
*/
if(item.isFormField()) {
// out.println("File Name = "+item.getFieldName()+", Value = "+item.getString());
} else {
//Handle Uploaded files.
/* out.println("Field Name = "+item.getFieldName()+
", File Name = "+item.getName()+
", Content type = "+item.getContentType()+
", File Size = "+item.getSize());*/
/*
* Write file to the ultimate location.
*/
File file = new File(destinationDir,item.getName());
item.write(file);
HttpServletResponse res = (HttpServletResponse)response;
res.sendRedirect(Path);
}
//out.close();
}
}catch(FileUploadException ex) {
log("Error encountered while parsing the request",ex);
} catch(Exception ex) {
log("Error encountered while uploading file",ex);
}
}
}زادكم الله من علمه وفضله وتوفيقه.