如何在J2ee中上载该图像后在Html文件中显示图像 [英] How Do I Show A Image In A Html File After Uploading That Image In J2ee

查看:58
本文介绍了如何在J2ee中上载该图像后在Html文件中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在上传这些图片后显示图片文件..我有一个html文件和Servlet文件,我正在上传一些图片,但我想通过一个html页面显示这些图片,用户可以在那里查看它们。它是可能的?? PLZ指导我该怎么办?我正在发送html页面和servlet页面,PLZ帮助...



HTML文件



< html>

< body>

< form action =imagemethod =postenctype =multipart / form-data>

选择文件:< input type =filename =fname/>


< input type =submitvalue =upload />

< / form>

< / body>

< / html>



和Servlet文件



import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.List;



import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



import org.apache.commons.fileupload.FileItem;

import org.apache .commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;



/ **

*用于处理客户端文件上传的Java servlet。

*

* @author www.codejava.net

* /

公共类图像扩展HttpServlet {

私有静态最终长版serialVersionUID = 1L;



/ /上传存储文件的位置

private static final String UPLOAD_DIR =upload;



//上传设置

private static final int MEMORY_THRESHOLD = 1024 * 1024 * 3 ; // 3MB

private static final int MAX_FILE_SIZE = 1024 * 1024 * 40; // 40MB

private static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50; // 50MB



/ **

*收到文件上传提交后,解析请求阅读

*上传数据并将文件保存在磁盘上。

* /

protected void doPost(HttpServletRequest请求,
HttpServletResponse响应)抛出ServletException,IOException {

//检查请求是否实际包含上传文件

if(!ServletFileUpload.isMultipartContent(request)){

//如果不是,我们在此停止

PrintWriter writer = response.getWriter();

writer.println(错误:表单必须具有enctype = multipart / form-data。);

writer.flush();

返回;

}



/ /配置上传设置

DiskFileItemFactory factory = new DiskFileItemFactory();

//设置内存阈值 - 超出该阈值les存储在磁盘中

factory.setSizeThreshold(MEMORY_THRESHOLD);

//设置存储文件的临时位置

factory.setRepository(new File (System.getProperty(java.io.tmpdir)));



ServletFileUpload upload = new ServletFileUpload(factory);



//设置上传文件的最大大小

upload.setFileSizeMax(MAX_FILE_SIZE);



//设置最大值请求的大小(包括文件+表单数据)

upload.setSizeMax(MAX_REQUEST_SIZE);



//构造商店上传的目录路径文件

//此路径相对于应用程序的目录

字符串uploadPath = getServletContext()。getRealPath()

+ File.separator + UPLOAD_DIR;



//如果目录不存在则创建目录

文件uploadDir =新文件(uploadPath);

if(!uploadDir.exists()){

uploadDir.mkdir();

}



尝试{

//解析请求的内容以提取文件数据

@SuppressWarnings (未选中)

列表< fileitem> formItems = upload.parseRequest(request);



if(formItems!= null&& formItems.size()> 0){

//遍历表单的字段

for(FileItem item:formItems){

//仅处理非表单字段的字段

if(!item.isFormField()){

String fileName = new File(item.getName())。getName();

String filePath = uploadPath + File .separator + fileName;

文件storeFile =新文件(filePath);





item.write( storeFile);

request.setAttribute(message,

上传已成功完成!);

}

}

}

} catch(exception ex){

request.setAttribute(message,

有一个错误:+ ex.getMessage());

}



getServletContext()。getRequestDispatcher(/ message.jsp)。forward(request,response);

}

}



真的说我从互联网上获得了这个servlet代码..如果这段代码错了,请帮助... plz plz。 ..

I want to show image files after uploading these images..i have a html file and Servlet file where i'm uploading some images,but i want to show those images through a html page where user can view them..can it be possible??plz guide me what should i do??i'm sending html page and servlet page,plz help...

HTML file

<html>
<body>
<form action="image" method="post" enctype="multipart/form-data">
Select File:<input type="file" name="fname"/>

<input type="submit" value="upload"/>
</form>
</body>
</html>

and Servlet File

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
* A Java servlet that handles file upload from client.
*
* @author www.codejava.net
*/
public class image extends HttpServlet {
private static final long serialVersionUID = 1L;

// location to store file uploaded
private static final String UPLOAD_DIR = "upload";

// upload settings
private static final int MEMORY_THRESHOLD = 1024 * 1024 * 3; // 3MB
private static final int MAX_FILE_SIZE = 1024 * 1024 * 40; // 40MB
private static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50; // 50MB

/**
* Upon receiving file upload submission, parses the request to read
* upload data and saves the file on disk.
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// checks if the request actually contains upload file
if (!ServletFileUpload.isMultipartContent(request)) {
// if not, we stop here
PrintWriter writer = response.getWriter();
writer.println("Error: Form must has enctype=multipart/form-data.");
writer.flush();
return;
}

// configures upload settings
DiskFileItemFactory factory = new DiskFileItemFactory();
// sets memory threshold - beyond which files are stored in disk
factory.setSizeThreshold(MEMORY_THRESHOLD);
// sets temporary location to store files
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

ServletFileUpload upload = new ServletFileUpload(factory);

// sets maximum size of upload file
upload.setFileSizeMax(MAX_FILE_SIZE);

// sets maximum size of request (include file + form data)
upload.setSizeMax(MAX_REQUEST_SIZE);

// constructs the directory path to store upload file
// this path is relative to application's directory
String uploadPath = getServletContext().getRealPath("")
+ File.separator + UPLOAD_DIR;

// creates the directory if it does not exist
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}

try {
// parses the request's content to extract file data
@SuppressWarnings("unchecked")
List<fileitem> formItems = upload.parseRequest(request);

if (formItems != null && formItems.size() > 0) {
// iterates over form's fields
for (FileItem item : formItems) {
// processes only fields that are not form fields
if (!item.isFormField()) {
String fileName = new File(item.getName()).getName();
String filePath = uploadPath + File.separator + fileName;
File storeFile = new File(filePath);


item.write(storeFile);
request.setAttribute("message",
"Upload has been done successfully!");
}
}
}
} catch (Exception ex) {
request.setAttribute("message",
"There was an error: " + ex.getMessage());
}

getServletContext().getRequestDispatcher("/message.jsp").forward(request, response);
}
}

Truly speaking i got this servlet code from internet..if this code wrong plz help...plz plz ...

推荐答案

将任务视为单独的。



任务一:

使用您的servlet或您希望的任何方式上传文件。您应该控制文件名和位置以供以后使用。



任务二:在页面呈现后从服务器加载图像文件。



使用AJAX上传文件,您可以保留当前页面。

使用表格,你需要加载一个新页面,即使它是同一页面。



在任何一种情况下,你拥有图像文件的名称和位置。

AJAX:将图像文件上传到你想要的位置。

形式:当你调用时你就是新的页面,将图像文件的名称构建到标题中,并使用javaScript加载图像。
Consider the tasks as separate.

Task one:
upload the file with your servlet, or by any means you wish. You should control the file name and location for later use.

Task Two: load an image file from your server after the page has already rendered.

Use AJAX to upload the file and you can stay on your current page.
Use a form and you'll need to load a new page, even if it's the same page.

In either case, you have the image file's name and location.
AJAX: upload the image file by to the location where you want it to go.
forms: when you invoke you're new page, build the image files' name into the header and use javaScript to load the image.


这篇关于如何在J2ee中上载该图像后在Html文件中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆