My code is uploading an image and saving the image to my server, but I need to display the image in my jsp page.
The jsp for uploading image
uploadImage.jsp
pageEncoding="ISO-8859-1"%>
Image Uploadname="productForm" id="productForm">
Image Details | ||
Image Link: | ||
Servlet
UploadImage
public class UploadImage extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 2082405235440894340L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
File filenameImg;
List items = null;
try {
items = new ServletFileUpload(new DiskFileItemFactory())
.parseRequest(request);
} catch (FileUploadException e) {
throw new ServletException("Cannot parse multipart request.", e);
}
for (FileItem item : items) {
if (item.isFormField()) {
// Process regular form fields here the same way as
// request.getParameter().
// You can get parameter name by
item.getFieldName();
// You can get parameter value by item.getString();
} else {
try{
// Process uploaded fields here.
String filename = FilenameUtils.getName(item.getName());
// Get filename.
String path = GetWebApplicationPathServlet.getContext().getRealPath("/images");
File file = new File(path,filename);
//File file = new File("C:\\", filename);
// Define destination file.
item.write(file);
System.out.println("filename: "+filename);
System.out.println("file: "+file);
request.setAttribute("image", file);
filenameImg = file;
// Write to destination file.
// request.setAttribute("image", filename);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
// Show result page.
System.out.println("request"+request.getAttribute("image"));
response.setContentType("image/jpeg");
//request.getRequestDispatcher("result.jsp").forward(request, response);
String nextJSP = "/result.jsp";
RequestDispatcher dispatcher = getServletContext()
.getRequestDispatcher(nextJSP);
dispatcher.forward(request, response);
}
}
result.jsp
pageEncoding="ISO-8859-1"%>
Insert title here">
is returned as null in the result.jsp page
Help
解决方案
I'm considering you are storing a image file somewhere on the server -
If that path is inside your application then you can give that path directly in the image src attribute, e.g. -
if you're storing file somewhere like /resources/image/someImage.jpg then you can give that path (path should be inside the application which is accessible via browser with your context rrot) image src attribute and browser will point to that image.
You can store the file somewhere in the server (outside of the application say "d:\someImage.jpg") then this cannot be reachable by the browser directly but you can still show that image via servlet, e.g. -
you can write a new servlet and in the goGet method you can read the image file and write it to the response output stream. give this servlet url in the src of the image like -
e.g.
try{
String fileName = request.getParameter("image");
FileInputStream fis = new FileInputStream(new File("d:\\"+fileName));
BufferedInputStream bis = new BufferedInputStream(fis);
response.setContentType(contentType);
BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream());
for (int data; (data = bis.read()) > -1;) {
output.write(data);
}
}
catch(IOException e){
}finally{
// close the streams
}
You can improve this code, I'm just giving an example.