开发者

How can i read the filename from jsp and use that as a src for image?

<%
    System.out.println("Content Type ="+request.getContentType());
String fileBasePath = "c:/temp"; //Base path where you wanto store the files...
    DiskFileUpload fu = new DiskFileUpload();
    fu.setSizeMax(100000000);

    List file开发者_开发问答Items = fu.parseRequest(request);
    Iterator itr = fileItems.iterator();

try{
    while(itr.hasNext()) {
      FileItem fi = (FileItem)itr.next();

      if(!fi.isFormField()) {
            File fNew= new File(fileBasePath, new File(fi.getName()).getName());
        System.out.println(fNew.getAbsolutePath());
        fi.write(fNew);
      }
      else {
        System.out.println("Field ="+fi.getFieldName());
      }
    }
}
catch(Exception e)
{
    System.out.println(e);
}

%>


Create a Servlet which gets the image as InputStream and writes it to the OutputStream of the response and then call that servlet in the <img src>.

E.g.

<img src="imageservlet/name.gif" />

with

InputStream input = new FileInputStream(new File("c:/temp", request.getPathInfo()));
OutputStream output = response.getOutputStream();
// Write input to output.

A basic example can be found here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜