我已在亚马逊war
上传了我的ec2-instance
文件和数据库。并试图在jsp页面上显示来自tomcat7 webapps的图像。我在图片/webapps/images/
中设置路径src
以动态显示图片。
当我点击上传图片时。在路径中上传的图像保存在数据库中。我从数据库中获取了图像路径。图像没有显示出来。
请帮我解决这个问题.....
修改
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(!ServletFileUpload.isMultipartContent(request)){
throw new ServletException("Content type is not multipart/form-data");
}
List<UploadBean> list = new ArrayList<UploadBean>();
int count=0;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
@SuppressWarnings("unchecked")
List<FileItem> fileItemsList = uploader.parseRequest(request);
Iterator<FileItem> fileItemsIterator = fileItemsList.iterator();
while(fileItemsIterator.hasNext()){
FileItem fileItem = fileItemsIterator.next();
System.out.println("FieldName="+fileItem.getFieldName());
System.out.println("FileName="+fileItem.getName());
System.out.println("ContentType="+fileItem.getContentType());
System.out.println("Size in bytes="+fileItem.getSize());
File file = new File(request.getServletContext().getAttribute("FILES_DIR")+File.separator+fileItem.getName());
System.out.println("Absolute Path at server="+file.getAbsolutePath());
fileItem.write(file);
SelectImageClass select = new SelectImageClass();
insertTempExp(fileItem.getName());
list= select.getUploadedImage();
request.setAttribute("serv", list);
request.getRequestDispatcher("/test.jsp").forward(
request, response);
}
} catch (FileUploadException e) {
out.write("Exception in uploading file.");
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:2)
显示您的jsp和java文件,以查找您用于访问图像的路径 服务器
只需使用它来上传路径
String rootPath=request.getSession().getServletContext().getRealPath("/");
String relativePath="images"; //folder name to save images
File file = new File(rootPath + File.separator + relativePath);
if(!file.exists()){ file.mkdirs();}
ctx.setAttribute("FILES_DIR_FILE", file);
ctx.setAttribute("FILES_DIR", rootPath + File.separator + relativePath);
并使用jsp显示图像
<image src="images/image_name">
答案 1 :(得分:0)
public class FileLocationContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent servletContextEvent) {
String rootPath = System.getProperty("catalina.home");
ServletContext ctx = servletContextEvent.getServletContext();
String relativePath = ctx.getInitParameter("/webapps/CovetloFiles.dir");
File file = new File(rootPath + File.separator + relativePath);
if(!file.exists()){ file.mkdirs();}
if(!file1.exists()){ file1.mkdirs();}
System.out.println("File Directory created to be used for storing files");
ctx.setAttribute("FILES_DIR_FILE", file);
ctx.setAttribute("FILES_DIR", rootPath + File.separator + relativePath);
}
<%
ArrayList<UploadBean> list = (ArrayList<UploadBean>) request.getAttribute("serv");
if(list!=null) {
for(UploadBean upload : list) {
strGet = upload.getUploadImage();
%>
<img class="img-responsive" src="/CovetloFiles/<%=upload.getUploadImage()%>" name="imgUrl" alt="prod1">
答案 2 :(得分:0)
上面使用servlet上下文我设置了上传图像的路径
下面我创建了java类,其中我从数据库中获取图像名称,只在上面路径上传的图像存储在数据库中的名称
我使用过:request.setAttribute(“serv”,list); list包含数据库表中的所有名称
答案 3 :(得分:0)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(!ServletFileUpload.isMultipartContent(request)){
throw new ServletException("Content type is not multipart/form-data");
}
List<UploadBean> list = new ArrayList<UploadBean>();
//HttpSession session = request.getSession(true);
int count=0;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
@SuppressWarnings("unchecked")
List<FileItem> fileItemsList = uploader.parseRequest(request);
Iterator<FileItem> fileItemsIterator = fileItemsList.iterator();
while(fileItemsIterator.hasNext()){
FileItem fileItem = fileItemsIterator.next();
System.out.println("FieldName="+fileItem.getFieldName());
System.out.println("FileName="+fileItem.getName());
System.out.println("ContentType="+fileItem.getContentType());
System.out.println("Size in bytes="+fileItem.getSize());
File file = new File(request.getServletContext().getAttribute("FILES_DIR")+File.separator+fileItem.getName());
System.out.println("Absolute Path at server="+file.getAbsolutePath());
// out.write("<a href=\"UploadServlet?fileName="+fileItem.getName()+"\">Download <img src=/CovetloFiles/"+fileItem.getName()+" width=100px height=100px></a>");
fileItem.write(file);
SelectImageClass select = new SelectImageClass();
insertTempExp(fileItem.getName());
list= select.getUploadedImage();
request.setAttribute("serv", list);
request.getRequestDispatcher("/test.jsp").forward(
request, response);
}
} catch (FileUploadException e) {
out.write("Exception in uploading file.");
} catch (Exception e) {
e.printStackTrace();
}
}