السلام عليكم ورحمة الله و بركاته
انا عندي صفحة servlet & JSP
وبحاول اعمل فورمة ابولد لصورة مع نص كتابي كالوصف مثلا
لكن المشكلة اني عندما احاول احضار المتغيرات في ال Servlet باستخدام request.getParameter
تظهرلي مشكلة و هي انها تكون ب Null
وبعد البحث وجدت اني عندما اضع Annotation
@MultiPartConfig
قيم المتغيرات تكون مقرؤة عادي جدا
لكن هنالك مشكلة جديدة ظهرت و هي انه في كود الابلود يدخل في لووب غير منتهي حتي مع زيادة المتغير كل مرة
وها هو الكود :
@MultipartConfig
public class upl extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//-------------------------------------
// Part imageLinkPart = request.getPart("imageLink");
// Scanner scanner = new Scanner(imageLinkPart.getInputStream());
// String imgLink = scanner.nextLine();
// //-------------------------------------
// MultipartParser mp=new MultipartParser(request, 1*1024*1024);
String imageDescription = request.getParameter("imageDesc");
String imageLink = request.getParameter("imageLink");
//to get the content type information from JSP Request Header
String contentType = request.getContentType();
//here we are checking the content type is not equal to Null and as well as the passed data from mulitpart/form-data is greater than or equal to 0
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
//we are taking the length of Content type data
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//================================================================
هذه هي اللووب غير المنتهية
//this loop converting the uploaded file into byte code
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
SQLOperations.connect();
String file = new String(dataBytes);
//for saving the file name
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1, contentType.length());
int pos;
SQLOperations.insertImageName(saveFile, imageDescription, imageLink);
//extracting the index of file
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;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
// creating a new file with the same name and writing the content in new file
String fileLocation = "C:\\Users\\M\\Music\\Documents\\NetBeansProjects\\Templ\\web\\upl\\" + saveFile;
FileOutputStream fileOut = new FileOutputStream(fileLocation);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
SQLOperations.disconnect();
response.sendRedirect("showImage.jsp");
}
// else{
// String error ="nullImage";
// response.sendRedirect("upload.jsp?error='"+error+"'&&imgDesc='"+imageDescription+"'");
// }
} finally {
out.close();
}
}[
[[/font
