j2ee中怎么通过webservice实现图片的传送预览?一般的要传递图片最好用什么方式实现? 5
4个回答
展开全部
这个小练习有一个图片展示工能,但我测试过了,IE6.0为核心的能看到图片,6.0之后就不能看到图片了
file的value的值比来是可以获得的,但由于安全的关系,在后来的版本中,不前获得他的路径了。
==========================上传图片的页面=============
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>上传</title>
</head>
<script type="text/javascript">
function createImg() {
var i=document.getElementById("imgid").value;
document.getElementById("imgid2").src=i;
alert(document.getElementById("imgid2").src);
}
</script>
<body>
<form action="servlet/SmartUploadTest" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="name" id="imgid" onchange="createImg()"/>
<img src="D:\basketball.png" id="imgid2"/>
名称:<input type="text" name="info"/>
<input type="submit" name="submit" value="上传">
</form>
</body>
</html>
=========================使有servlet,上传图片的class文件=================
package study1;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class SmartUploadTest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String filePath="image/";
String messages="";
String forward="";
SmartUpload su=new SmartUpload();
long maxsize=2*1024*1024;
//设置每个上传文件的大小,为2MB;
String allFileList="jpg,gif,bmp,png";
String denidFilesList="exe,bat,jsp,htm,html,,";
su.initialize(this.getServletConfig(),request,response);
//初始化
su.setMaxFileSize(maxsize);
//限制上传文件的大小
su.setAllowedFilesList(allFileList);
//设置允许上传的文件类型
try {
su.setDeniedFilesList(denidFilesList);
su.upload();//上传文件;
String photoInfo=su.getRequest().getParameter("info");
//必须这样来获取request
if(photoInfo==null||photoInfo.equals("")){
//验证照片描述信息,若没有输入,则提示输入照片描述信息
messages="请输入照片描述信息!!";
forward="/error.jsp";
}else{
File file=su.getFiles().getFile(0);
//获取上传的文件,因为只上传了一个文件,所以可直接获取。
if(!file.isMissing()){
//如果选择了文件
String now=new Date().getTime()+"";
//获取当前时间并格式化为字符串
String photoAddr=filePath+now+"."+file.getFileExt();
//filepaht值
file.saveAs(photoAddr,File.SAVEAS_VIRTUAL);
request.getRequestDispatcher("/success.jsp").forward(request, response);
return;
}else{
messages="请选择要上传的文件!!";
forward="/error.jsp";
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SmartUploadException e) {
messages="上传文件失败!!";
e.printStackTrace();
}catch(java.lang.SecurityException e){
messages="上传文件失败!!上传的文件类型只允许为:jpg,gif,png,bmp";
forward="/error.jsp";
}
request.setAttribute("messages",messages);
request.getRequestDispatcher(forward).forward(request, response);
}
}
==========================上传失败的展示页面==================
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>错误页面</title>
</head>
<style>
td{
color='red';
vertical-align:middle;
text-align=center;
font-size:30px;
font-weight:bold;
}
table{
border:solid 2px red;
height=80px;
width=80px;
background:gray;
}
</style>
<body>
<center>
<table >
<tr>
<td>
<strong>错误</strong>
</td>
</tr>
</table>
<%
out.print(request.getAttribute("messages"));
%>
</center>
</body>
</html>
========================上传成功的显示页面==============================
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>上传成功</title>
</head>
<style>
div{
border:solid 3px red;
font-weight:bold;
font-size:60px;
width:500px;
vertical-align:middle;
}
</style>
<body>
<center><div>上传成功</div></center>
</body>
</html>
file的value的值比来是可以获得的,但由于安全的关系,在后来的版本中,不前获得他的路径了。
==========================上传图片的页面=============
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>上传</title>
</head>
<script type="text/javascript">
function createImg() {
var i=document.getElementById("imgid").value;
document.getElementById("imgid2").src=i;
alert(document.getElementById("imgid2").src);
}
</script>
<body>
<form action="servlet/SmartUploadTest" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="name" id="imgid" onchange="createImg()"/>
<img src="D:\basketball.png" id="imgid2"/>
名称:<input type="text" name="info"/>
<input type="submit" name="submit" value="上传">
</form>
</body>
</html>
=========================使有servlet,上传图片的class文件=================
package study1;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class SmartUploadTest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String filePath="image/";
String messages="";
String forward="";
SmartUpload su=new SmartUpload();
long maxsize=2*1024*1024;
//设置每个上传文件的大小,为2MB;
String allFileList="jpg,gif,bmp,png";
String denidFilesList="exe,bat,jsp,htm,html,,";
su.initialize(this.getServletConfig(),request,response);
//初始化
su.setMaxFileSize(maxsize);
//限制上传文件的大小
su.setAllowedFilesList(allFileList);
//设置允许上传的文件类型
try {
su.setDeniedFilesList(denidFilesList);
su.upload();//上传文件;
String photoInfo=su.getRequest().getParameter("info");
//必须这样来获取request
if(photoInfo==null||photoInfo.equals("")){
//验证照片描述信息,若没有输入,则提示输入照片描述信息
messages="请输入照片描述信息!!";
forward="/error.jsp";
}else{
File file=su.getFiles().getFile(0);
//获取上传的文件,因为只上传了一个文件,所以可直接获取。
if(!file.isMissing()){
//如果选择了文件
String now=new Date().getTime()+"";
//获取当前时间并格式化为字符串
String photoAddr=filePath+now+"."+file.getFileExt();
//filepaht值
file.saveAs(photoAddr,File.SAVEAS_VIRTUAL);
request.getRequestDispatcher("/success.jsp").forward(request, response);
return;
}else{
messages="请选择要上传的文件!!";
forward="/error.jsp";
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SmartUploadException e) {
messages="上传文件失败!!";
e.printStackTrace();
}catch(java.lang.SecurityException e){
messages="上传文件失败!!上传的文件类型只允许为:jpg,gif,png,bmp";
forward="/error.jsp";
}
request.setAttribute("messages",messages);
request.getRequestDispatcher(forward).forward(request, response);
}
}
==========================上传失败的展示页面==================
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>错误页面</title>
</head>
<style>
td{
color='red';
vertical-align:middle;
text-align=center;
font-size:30px;
font-weight:bold;
}
table{
border:solid 2px red;
height=80px;
width=80px;
background:gray;
}
</style>
<body>
<center>
<table >
<tr>
<td>
<strong>错误</strong>
</td>
</tr>
</table>
<%
out.print(request.getAttribute("messages"));
%>
</center>
</body>
</html>
========================上传成功的显示页面==============================
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>上传成功</title>
</head>
<style>
div{
border:solid 3px red;
font-weight:bold;
font-size:60px;
width:500px;
vertical-align:middle;
}
</style>
<body>
<center><div>上传成功</div></center>
</body>
</html>
展开全部
增加协议支持多媒体文件就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用二进制流实现图片传输吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
- - 文件流?。。不晓得~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询