文件名:<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.这样就可以了.....
你去网上下载一个smartUpload.jar,然后把这个import到你的处理页面或者是Servlet中,例如:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ page import="com.jspsmart.upload.*" %>
<%@page import="s2jsp.bysj.entity.Product"%>
<%@page import="s2jsp.bysj.dao.ProductDao"%>
<%@page import="s2jsp.bysj.dao.impl.ProductDaoImpl"%><%
SmartUpload su=new SmartUpload()
su.initialize(pageContext)
su.upload()
int count = su.save("/image")
Request req = su.getRequest()
String serialNumber= req.getParameter("serialNumber")
String name=req.getParameter("name")
String brand=req.getParameter("brand")
String model=req.getParameter("model")
String price=req.getParameter("price")
String description=req.getParameter("description")
com.jspsmart.upload.File file = su.getFiles().getFile(0)
String picture=file.getFileName()
Product product=new Product()
product.setSerialNumber(serialNumber)
product.setName(name)
product.setBrand(brand)
product.setModel(model)
product.setPrice(price)
product.setPicture(picture)
product.setDescription(description)
ProductDao dao=new ProductDaoImpl()
int res=dao.addProduct(product)
if (res!=1)
{
out.print("<script>alert('添加失败。')location.href='addProduct.html'</script>")
return
}
out.print("<script>alert('添加成功。')location.href='manageProduct.jsp'</script>")
%>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)