java jsp显示图片,在servlet的jsp页面中显示图像

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 Upload

name="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

<%=request.getParameter(">

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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值