当前位置:首页 » 文件管理 » 上传的文件显示在jsp

上传的文件显示在jsp

发布时间: 2023-01-08 22:00:16

① 怎样将上传的文件名称显示jsp页面上

... 重命名卜知道``

你上传文件的时候一般是把路径存到数据库嘛``而且那个相对路径是固定的
那你把路径存到数据库之前把把那路径截取下来 只把文件名存到数据库嘛``
去取文件名的时候再把相对路径加上去阿``
这样的话你数据库就只存了文件名

这样你直接把数据库的文件名显示到JSP页面卜就可以了``

② 1、struts2 如何上传文件并显示在jsp页面上,提供下载连接 2、struts2 如何下载显示在jsp页面上的文件

http://hi..com/kawnj19890209/blog/item/8c64d78e139c88f4f11f36fa.html可以看看这儿

java中如何将文件的内容在显示在jsp上,也就是在页面上显示!谢啦!

首先先把文件的内容读出来,然后封装成一个对象或是直接用字符传送到页面上,之后就可以在页面上显示了。其实主要还是怎么从文件中读出内容,怎么传递到页面上,希望你自己考虑下,然后就成了。

④ jsp文上传如何实现将上传的多个文件都同时显示出来

需要写一个servlet来处理上传的文件,你可以修改保存路径或选择将图片保存在数据库中,只需要做简单的修改就行了,servlet代码如下:
package com.ek.servlet;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.ek.image.ImageUtil;
publicclass FileUploadServlet extends HttpServlet {
/**
*
*/
privatestaticfinallong serialVersionUID = 1L;
privatestatic String filePath = "";
/**
* Destruction of the servlet.

*/
publicvoid destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doPost method of the servlet.

*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
publicvoid doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html; charset=UTF-8");
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
factory.setRepository(new File(filePath));
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum size before a FileUploadException will be thrown
upload.setSizeMax(1000000);
try {
List fileItems = upload.parseRequest(req);
Iterator iter = fileItems.iterator();
// Get the file name
String regExp = ".+\\\\(.+\\.?())$";
Pattern fileNamePattern = Pattern.compile(regExp);
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if ((name == null || name.equals("")) && size == 0)
continue;
Matcher m = fileNamePattern.matcher(name);
boolean result = m.find();
if (result) {
try {
// String type =
// m.group(1).substring(m.group(1).lastIndexOf('.')+1);
InputStream stream = item.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = newbyte[1000];
while (stream.read(b) > 0) {
baos.write(b);
}
byte[] imageByte = baos.toByteArray();
String type = ImageUtil.getImageType(imageByte);
if (type.equals(ImageUtil.TYPE_NOT_AVAILABLE))
thrownew Exception("file is not a image");
BufferedImage myImage = ImageUtil
.readImage(imageByte);
// display the image
ImageUtil.printImage(myImage, type, res
.getOutputStream());
// save the image
// if you want to save the file into database, do it here
// when you want to display the image, use the method printImage in ImageUtil
item.write(new File(filePath + "\\" + m.group(1)));

stream.close();
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
throw new IOException("fail to upload");
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (FileUploadException e) {
e.printStackTrace();
}
}
/**
* Initialization of the servlet.

*
* @throws ServletException
* if an error occure
*/
public void init() throws ServletException {
// Change the file path here
filePath = getServletContext().getRealPath("/");
}
}
servlet中使用到一个ImageUtil类,其中封装了图片处理的实用方法,用于读写图片,代码如下:
package com.ek.image;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.MemoryCacheImageInputStream;
import net.jmge.gif.Gif89Encoder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sun.imageio.plugins.bmp.BMPImageReader;
import com.sun.imageio.plugins.gif.GIFImageReader;
import com.sun.imageio.plugins.jpeg.JPEGImageReader;
import com.sun.imageio.plugins.png.PNGImageReader;
/**
* @author Erick Kong
* @see ImageUtil.java
* @createDate: 2007-6-22
* @version 1.0
*/
publicclass ImageUtil {

publicstaticfinal String TYPE_GIF = "gif";
publicstaticfinal String TYPE_JPEG = "jpeg";
publicstaticfinal String TYPE_PNG = "png";
publicstaticfinal String TYPE_BMP = "bmp";
publicstaticfinal String TYPE_NOT_AVAILABLE = "na";
privatestatic ColorModel getColorModel(Image image)
throws InterruptedException, IllegalArgumentException {
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
if (!pg.grabPixels())
thrownew IllegalArgumentException();
return pg.getColorModel();
}
privatestaticvoid loadImage(Image image) throws InterruptedException,
IllegalArgumentException {
Component mmy = new Component() {
privatestaticfinallong serialVersionUID = 1L;
};
MediaTracker tracker = new MediaTracker(mmy);
tracker.addImage(image, 0);
tracker.waitForID(0);
if (tracker.isErrorID(0))
thrownew IllegalArgumentException();
}
publicstatic BufferedImage createBufferedImage(Image image)
throws InterruptedException, IllegalArgumentException {
loadImage(image);
int w = image.getWidth(null);
int h = image.getHeight(null);
ColorModel cm = getColorModel(image);
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferedImage bi = gc.createCompatibleImage(w, h, cm.getTransparency());
Graphics2D g = bi.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bi;
}
publicstatic BufferedImage readImage(InputStream is) {
BufferedImage image = null;
try {
image = ImageIO.read(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return image;
}

publicstatic BufferedImage readImage(byte[] imageByte) {
ByteArrayInputStream s = new ByteArrayInputStream(imageByte);
BufferedImage image = readImage(s);
return image;
}
publicstaticvoid encodeGIF(BufferedImage image, OutputStream out)
throws IOException {
Gif89Encoder encoder = new Gif89Encoder(image);
encoder.encode(out);
}
/**
*
* @param bi
* @param type
* @param out
*/
publicstaticvoid printImage(BufferedImage bi, String type,
OutputStream out) {
try {
if (type.equals(TYPE_GIF))
encodeGIF(bi, out);
else
ImageIO.write(bi, type, out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Get image type from byte[]
*
* @param textObj
* image byte[]
* @return String image type
*/
publicstatic String getImageType(byte[] textObj) {
String type = TYPE_NOT_AVAILABLE;
ByteArrayInputStream s = null;
MemoryCacheImageInputStream mcis = null;
try {
s = new ByteArrayInputStream(textObj);
mcis = new MemoryCacheImageInputStream(s);
Iterator itr = ImageIO.getImageReaders(mcis);
while (itr.hasNext()) {
ImageReader reader = (ImageReader) itr.next();
if (reader instanceof GIFImageReader) {
type = TYPE_GIF;
} elseif (reader instanceof JPEGImageReader) {
type = TYPE_JPEG;
} elseif (reader instanceof PNGImageReader) {
type = TYPE_PNG;
} elseif (reader instanceof BMPImageReader) {
type = TYPE_BMP;
}
reader.dispose();
}
} finally {
if (s != null) {
try {
s.close();
} catch (IOException ioe) {
if (_log.isWarnEnabled()) {
_log.warn(ioe);
}
}
}
if (mcis != null) {
try {
mcis.close();
} catch (IOException ioe) {
if (_log.isWarnEnabled()) {
_log.warn(ioe);
}
}
}
}
if (_log.isDebugEnabled()) {
_log.debug("Detected type " + type);
}
return type;
}
privatestatic Log _log = LogFactory.getLog(ImageUtil.class);
}
注意:对gif格式的图片进行处理的时候,需要另外一下jar包:gif89.jar,因为gif格式的图片不能使用ImageIO进行输出。
如需将图片存放到数据库中,只需要修改红色部分,将图片以blob格式保存到数据库中,显示时以byte[]格式读出,使用ImageUtil.printImage()进行输出。

⑤ jsp上传的文件怎样在网页中显示 或者下载

itjob为你解答:如果你上传在项目的目录中,可以提供相对路径显示,如果是上传在别的文件夹。就需要显示相对路径对应servlet。

⑥ JSP上传文本文件,并在页面显示其内容

jsp上传文本并显示内容:
<input type="file" onchange="onFileSelected(event)">
<textarea id="result"></textarea>
function onFileSelected(event) {
var selectedFile = event.target.files[0];
var reader = new FileReader();
var result = document.getElementById("result");
reader.onload = function(event) {
result.innerHTML = event.target.result;
};
reader.readAsText(selectedFile);
}

显示:
<c:import var="data"
url="http://www.example.com/file.txt"
scope="session"/>
<c:out value="${data}"/>

⑦ 用JSP显示上传后的文件列表并下载代码 大神帮帮忙啊

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@page import="java.io.IOException"%>
<%@page import="java.io.File"%>
<%
request.setCharacterEncoding("UTF-8");

//-----------------------------------------------------------
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String real=application.getRealPath("/");

String saveDir = application.getRealPath("")+"/Files/";
int maxPostSize = 2000*1024*1024;//
String encoding = "UTF-8";
String fileName = null;
String fileName2 = null;
MultipartRequest multi = null;
MultipartRequest multi2 = null;
try{
multi = new MultipartRequest(request,saveDir,maxPostSize,encoding);
}catch(IOException e){
return;
}

String ContentType="";
Enumeration filesname = multi.getFileNames();

String descs=multi.getParameter("descs");
String staffid=multi.getParameter("staffid");

String yuanName="";//文件原名称
double size=0.0d;//文件大小,以kb为单位
String error="";

while(filesname.hasMoreElements()){
try{
String name = (String)filesname.nextElement();
fileName = multi.getFilesystemName(name);
out.print("<br/>---"+fileName+"--<br/>");
yuanName=fileName.toString();
File f = multi.getFile(name);
size=f.length();
out.print("<br/>文件大小---"+size+"B--<br/>");
ContentType = fileName.substring(fileName.lastIndexOf(".")+1);
File file=new File(saveDir+fileName);
String newFileName="";

newFileName=String.valueOf(new java.util.Date().getTime())+"."+ContentType;
int fileid=0;
if(file.exists()){
file.renameTo(new File(saveDir+newFileName));
%>
<a href="<%=basePath %>Files/<%=newFileName%>">下载</a>
<%

}
else{
error="请选择要上传的文件";

}

}catch(Exception e){ //跳回去
error="请选择要上传的文件22";
}

}

%>
</body>
</html>

⑧ j2ee项目web开发用户上传头像jsp页面显示问题

1、乱码把<%@page中的charset=UTF-8
2、文件上传最简单的方式:页面用<file>标签选择文件,from提交方式method=multipart/from-data这样servlet可以直接获取到页面的文件输入输出流,这样你可以保存到服务器,D盘啊随意什么位置!
3、servlet想页面输出文件,可以获取jsp的out对象,直接输出页面例如:out.write("img<src='路径'>");即可

⑨ 如何向服务器上传一个PPT文件,再将其在jsp中显示出来,我知道显示是用iframe标签

我建议你用iSpring软件把PPT转成交互效果相同的Flash然后放在页面上显示,不然你自己写代码解析PPT,然后自己写组件显示效果,太麻烦了。

⑩ 图片上传后可以在页面上显示出来,那上传的word文档如何在JSP页面上显示出来呢~~~

一般一个Word或Excel文档应该先编辑成html页面再在网站里面使用吧,你直接使用word,用户只能下载后才能看到内容,不能在线浏览呢。

热点内容
php如何获取url 发布:2025-08-29 22:51:13 浏览:841
194起床战争服务器ip 发布:2025-08-29 22:51:00 浏览:224
ktv服务器提成怎么算 发布:2025-08-29 22:48:09 浏览:363
电脑死机脚本 发布:2025-08-29 22:38:37 浏览:759
服务器上的ip怎么跑爬虫 发布:2025-08-29 22:34:41 浏览:678
安卓去哪里免费下2k21 发布:2025-08-29 22:13:53 浏览:804
python编程能做什么 发布:2025-08-29 22:11:17 浏览:355
数据库网页设计 发布:2025-08-29 21:53:03 浏览:367
手机密码忘了怎么解开 发布:2025-08-29 21:52:24 浏览:413
微信云控脚本 发布:2025-08-29 21:48:01 浏览:448