JSP如何上传图片?

JSP如何上传图片?,第1张

如果你是纯JSP写的,可以用SmartUpload.在你的页面form 里<form action="doUpload.jsp" method="POST" enctype="multipart/form-data">

文件名:<input type="text" name="name"/><br>

请选择上传文件:<input type="file" name="file1"/>

<input type="submit" value="上传"/>

</form>注意:enctype="multipart/form-data"这个一定要这样设置,具体什么意思我也不是很清楚.....(呵呵) 提交到执行的页面如下: //实例化上传组件

SmartUpload upload = new SmartUpload()

//初始化上传组件

upload.initialize(this.getServletConfig(), request, response)

//开始上传

upload.upload()

//获取上传的文件列表对象

Files f = upload.getFiles()

//获取文件对象

File fil = f.getFile(0)

//去掉文件后缀

String ext = fil.getFileExt()

//判断文件类型是否是jpg格式jpg,gif,bmp,png,JPG,GIF,BMP,PNG

if (!(ext.equals("jpg")) &&!(ext.equals("gif")) &&!(ext.equals("bmp")) &&!(ext.equals("png")) &&!(ext.equals("JPG")) &&!(ext.equals("GIF")) &&!(ext.equals("BMP")) &&!(ext.equals("PNG"))) {

out.println("<script type='text/javascript'>alert('文件类型错误')location.replace('upLoadPhoto.jsp')</script>")

return

}

//满足条件进行文件的上传uploadImages在webRoot文件夹下的一个目录

fil.saveAs("uploadImages/" + fil.getFileName())

String filepath = "uploadImages/" + fil.getFileName() //保存到数据库的路径 OK.这样就可以了.....

1.限制文件上传类型只能是图片

function checkFileType(name,file){

var extArray = new Array(".doc",".docx")

var allowSubmit = false

if (!file){

return

}

while (file.indexOf("\\") != -1){

file = file.slice(file.indexOf("\\") + 1)

}

var ext = file.slice(file.indexOf(".")).toLowerCase()

for (var i = 0i <extArray.lengthi++) {

if (extArray[i] == ext){

allowSubmit = true

break

}

}

if(!allowSubmit){

alert("只能上传以下格式的文件:"+ (extArray.join("")) + "\n请重新选择再上传.")

document.getElementById(name).value = ""

}

}

其中:extArray是要求文件类型。可自行定义。

2.引入jQuery外部文件

jquery-2.1.4.min.js

3.编写js代码

$(function () {

$('#txtfilePath1').uploadReview({

width: 350,

height: 350,

target: '#uploadReview1_content'

})

})

其中:txtfilePath1是input:file。width,height是预览图片的宽度和高度。target是显示预览图片的位置。

4.编写jsp页面代码

<body>

<input type="text" class="yourClassName" name="filePath1" id="filePath1"/>

<input type="file" id="txtfilePath1" name="txtfilePath1" style="display:none">

<input type="button" onclick="txtfilePath1.click()" id="fileup1" name="fileup1" class="searchThing"value="上传">

</body>

注: 这个是很久以前在网上看到的,就整理了下来,但是这么久都没用过,所以也没调试过,你自己试一试研究研究, 再来网上很多博客里,他们写的很详细的,可以去看看

upfile.jsp 文件代码如下:

<form method="post" action="uploadimage.jsp" name="form1" enctype="multipart/form-data">

<input type="file" name="file">

<input type="submIT" name="sub" value="upload">

</form>

<form method="post" action="uploadimage.jsp" name="form1" enctype="multipart/form-data">

<input type="file" name="file">

<input type="submit" name="sub" value="upload">

</form>

<STRONG><FONT color=#ff0000>uploadimage.jsp</FONT></STRONG>

文件代码如下:

uploadimage.jsp

文件代码如下:view plaincopy to clipboardprint?

<PRE class=java name="code"><%@ page language="java" pageEncoding="gb2312"%>

<%@ page import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,java.sql.*,com.jspsmart.upload.*,java.util.*"%>

<%@ page import="mainClass.*" %>

<html>

<head>

<title>My JSP 'uploadimage.jsp' starting page</title>

</head>

<body>

<%

SmartUpload sma=new SmartUpload()

long file_max_size=4000000

String filename1="",ext="",testvar=""

String url="uploadfiles/"

sma.initialize(pageContext)

try

{

sma.setAllowedFilesList("jpg,gif")

sma.upload()

}catch(Exception e){

%>

<script language="jscript">

alert("只允许上传jpg,gif图片")

window.location.href="upfile.jsp"

</script>

<%

}

try{

com.jspsmart.upload.File myf=sma.getFiles().getFile(0)

if(myf.isMissing()){

%>

<script language="jscript">

alert("请选择要上传的文件!")

window.location.href="upfile.jsp"

</script>

<%

}else{

ext=myf.getFileExt()

int file_size=myf.getSize()

String saveurl=""

if(file_size <file_max_size){

Calendar cal=Calendar.getInstance()

String filename=String.valueOf(cal.getTimeInMillis())

saveurl=request.getRealPath("/")+url

saveurl+=filename+"."+ext

myf.saveAs(saveurl,sma.SAVE_PHYSICAL)

myclass mc=new myclass(request.getRealPath("data/data.mdb"))

mc.executeInsert("insert into [path] values('uploadfiles/"+filename+"."+ext+"')")

out.println("图片上传成功!")

response.sendRedirect("showimg.jsp")

}

}

}catch(Exception e){

e.printStackTrace()

}

%>

</body>

</html>

</PRE>

本文来自: IT知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/java/jsp/20080916/2409.html


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12126996.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存