溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

jsp上傳顯示圖片的方法

發(fā)布時(shí)間:2020-09-16 11:48:26 來(lái)源:億速云 閱讀:234 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹jsp上傳顯示圖片的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

之前做過(guò)一個(gè)項(xiàng)目,有一個(gè)功能是圖片上傳并且展示圖片,嘗試過(guò)其他的方法,但會(huì)有一個(gè)問(wèn)題,那就是在IE8上圖片并不能下常展示,

所以便用以下方法來(lái)上傳圖片,很好的解決了此問(wèn)題,步驟如下:

1.上傳圖片頁(yè)面index.jsp

<%@ page language="java" import="java.util.*,java.net.URLDecoder" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</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">
	<script  type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
	<script  type="text/javascript" src="js/ajaxupload.js"></script>
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>
<body>
<script type="text/javascript">
$(function(){
	//上傳圖片
	new AjaxUpload('#addLabProdPic', {
		action: 'upload.jsp',
		name: 'picFile',
		responseType: 'json',
		onSubmit : function(file , ext){
			if (ext && /^(jpg|png|bmp)$/.test(ext.toLowerCase())){
				this.setData({
					'picName': file
				});
			} else {
				alert("請(qǐng)上傳格式為 jpg|png|bmp 的圖片!");
				return false;				
			}
		},
		onComplete : function(file,response){
			if(response.error) {
				alert(response.error);
				return;
			}
			//alert(response.picUrl);
			show(response.picUrl);
		}		
	});
})
function show(path){
	
if(document.all)//IE
{
//path = "D:/upload/11.png";
document.getElementById("imgPreview").innerHTML="";
document.getElementById("imgPreview").style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='scale',src=\"" + path + "\")";//使用濾鏡效果www.2cto.com  
}
else//FF
{
//path = "D:/upload/11.png";
//document.getElementById("imgPreview").innerHTML = "<img id='img1' width='120px' height='100px' src='"+path+"'/>";
document.getElementById("img1").src = path;
}
};
</script>
<h2>Ajax文件上傳例子,選擇圖片后立即上傳,無(wú)需點(diǎn)擊上傳按鈕</h2>
	<button id="addLabProdPic">選擇圖片</button>
	<br>
	<div id="imgPreview" style='width:120px; height:100px;'>
	<img id="viewImg"  width="200px" height="200px;">
	</div>
</body>
</html>

2.上傳圖片后臺(tái)處理業(yè)務(wù)upload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.io.PrintWriter,java.io.File,org.apache.commons.fileupload.FileItem,org.apache.commons.fileupload.disk.DiskFileItemFactory,org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%
System.out.println("///////");
// dfif對(duì)象為解析器提供解析時(shí)的缺省的一些配置
	DiskFileItemFactory dfif = new DiskFileItemFactory();
// 創(chuàng)建解析器
	ServletFileUpload sfu = new ServletFileUpload(dfif);
sfu.setHeaderEncoding("utf-8");//解決了上傳圖片如果為中文就是亂碼問(wèn)題
String loadpath="D:/upload";//上傳文件存放目錄(此路徑是將上傳的文件放在本地的硬盤上)
String filName="";
try{
	// 開始解析(分析InputStream)
	// 每一個(gè)表單域當(dāng)中的數(shù)據(jù)都會(huì)
	// 封裝到一個(gè)對(duì)應(yīng)的FileItem對(duì)象上。
	List<FileItem> items = sfu.parseRequest(request);
	for (int i = 0; i < items.size(); i++) {
		FileItem item = items.get(i);
		// 要區(qū)分是上傳文件域還是普通的表單域
		if (item.isFormField()) {
					// 普通表單域
					String name = item.getString();
					filName=name;
					System.out.println("name:" + name);
				} else {
					// 上傳文件域
					// ServletContext:servlet上下文對(duì)象。
					ServletContext sctx = getServletContext();
					// 獲得原始的文件名
					String filename = item.getName();
			File loadFolder = new File(loadpath);
			if (!loadFolder.exists()) {
				loadFolder.mkdirs();
			}
					File file = new File(loadFolder + "\\" + filename);
					item.write(file);
				}
	}
	String result=loadpath+"/"+filName;
	PrintWriter writer = response.getWriter();
	
	writer.print("{");
	//writer.print("msg:\"文件大小:"+item.getSize()+",文件名:"+filename+"\"");
	writer.print("picUrl:\"" + result + "\"");
	writer.print("}");
	
	writer.close();
}catch(Exception e){
	e.printStackTrace();
}
%>

3.所需主要文件ajaxupload.js

這部分缺失,導(dǎo)致交互出現(xiàn)問(wèn)題,所以需要大家摸索了,加油。

以上是jsp上傳顯示圖片的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

jsp
AI