form============================================================
<form action="addAuction" enctype="multipart/form-data" method="post" οnsubmit="return checkValue()">
<dd>
<label>起拍价:</label>
<input type="text" οnblur="checkStartPrice()" name="startPrice" class="inputh lf" value="" />
<div id="startPriceid" class="lf red laba">必须为数字</div>
</dd>
-----------------------------------
<dd>
<label>开始时间:</label>
<input type="text" name="startTime" class="inputh lf" value="" οnblur="checkStartTime()"/>
<div id="startTimeid" class="lf red laba">格式:2010-05-05 12:30:00</div>
</dd>
-----------------------------------
<dd class="dds">
<label>拍卖品图片:</label>
<div class="lf salebd"><img id="imgid" src="images/ad20.jpg" width="100" height="100" /></div>
<input name="pic" type="file" class="offset10 lf" οnchange="showPic()"/>
<div id="picid" class="lf red laba">请上传图片</div>
</dd>
-----------------------------------
<dd class="hegas">
<input type="submit" value="保 存" class="spbg buttombg buttombgs buttomb f14 lf" />
<input type="reset" value="取 消" class="spbg buttombg buttombgs buttomb f14 lf" />
</dd>
js验证============================================================
function checkStartPrice(){
if(($("startPrice").value=="")||(isNaN($("startPrice").value)==true)){
$("startPriceid").style.display="block";
return false;
}else{
$("startPriceid").style.display="none";
return true;
}
}
-----------------------------------
function checkStartTime(){
if($("startTime").value==""){
$("startTimeid").style.display="block";
return false;
}else{
$("startTimeid").style.display="none";
return true;
}
}
-----------------------------------
function checkValue(){
if((checkAuctionName()&&checkStartPrice()&&checkUpset()&&checkStartTime()&&checkEndTime())==true){
if($("pic").value==""){
$("picid").style.display="block";
return false;
}else{
$("picid").style.display="none";
return true;
}
}
return false;
}
-----------------------------------
function showPic(){
$("imgid").src=$("pic").value;/*考虑到安全性和兼容性问题,发布拍卖品时,图片可以不显示*/
}
实体类============================================================
public class Auction implements java.io.Serializable {
// Fields
private Integer auctionid;
private String auctionname;
private Double auctionstartprice;
private Double auctionupset;
private Timestamp auctionstarttime;
private Timestamp auctionendtime;
private Blob auctionpic;
private String auctionpictype;
private String auctiondesc;
private Set auctionrecords = new HashSet(0);
private String picName;//用来生成服务器端文件的路径
}
映射文件=picName无===========================================================
<property name="auctionpic" type="java.sql.Blob">
<column name="AUCTIONPIC" not-null="true" />
</property>
servlet实现增加的对象的属相(含图片)的上传数据库============================================================
if("add".equals(opr)){
String uploadFileName = ""; // 上传的文件名
String fieldName = ""; // 表单字段元素的name属性值
// 请求信息中的内容是否是multipart类型
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// 上传文件的存储路径(服务器文件系统上的绝对文件路径)
String uploadFilePath = request.getSession().getServletContext()
.getRealPath("upload/");
File fullFile = null;
String fileType=null;
Auction auction = new Auction();
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
// 解析form表单中所有文件
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) { // 依次处理每个文件
FileItem item = (FileItem) iter.next();
if (item.isFormField()) { // 普通表单字段
fieldName = item.getFieldName(); // 表单字段的name属性值
String value=item.getString("UTF-8");
// 获取表单字段的值
if (fieldName.equals("auctionName")) {
auction.setAuctionname(value);
}
if (fieldName.equals("startPrice")) {
auction.setAuctionstartprice(new Double(value));
}
if (fieldName.equals("upset")) {
auction.setAuctionupset(new Double(value));
}
if (fieldName.equals("startTime")) {
auction.setAuctionstarttime(new java.sql.Timestamp(Changetime.strdate(
value, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).getTime()));
}
if (fieldName.equals("endTime")) {
auction.setAuctionendtime(new java.sql.Timestamp(Changetime.strdate(
value, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).getTime()));
}
if (fieldName.equals("desc")) {
auction.setAuctiondesc(value);
}
} else { // 文件表单字段
String fileName = item.getName();
if (fileName != null && !fileName.equals("")) {
fullFile = new File(item.getName());
File saveFile = new File(uploadFilePath,
fullFile.getName());
item.write(saveFile);
uploadFileName = fullFile.getName();
fileType=uploadFileName.substring(uploadFileName.lastIndexOf(".")+1);
System.out.println("上传成功后的文件名是:" + uploadFileName);
}
}
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(auction);
System.out.println(auction.getAuctiondesc());
System.out.println(auction.getAuctionendtime());
System.out.println(auction.getAuctionpic());
System.out.println(auction.getAuctionrecords());
InputStream is = new FileInputStream(fullFile);
byte[] byteArray = new byte[is.available()];
is.read(byteArray);
is.close();
//设置图片数据
auction.setAuctionpic(Hibernate.createBlob(byteArray));
//设置图片类型
auction.setAuctionpictype(fileType);
gbi.save(auction);
response.sendRedirect("goodservlet?opr=list");
}
servlet用于将图片写入服务器,并将服务器端的路径付给picname,便于客户端显示图片============================================================
if("one".equals(opr)){//goodServlet?opr=one&gid=1
Integer gid =Integer.parseInt( request.getParameter("gid").trim() );
Auction g=gbi.findById(gid);
int i=0;
if(g.getAuctionstarttime().getTime()>new Date().getTime()){//1未开始
i=1;
}else if(g.getAuctionendtime().getTime()<new Date().getTime()){//2拍卖结束 3拍卖中
i=2;
}else{
i=3;
}
FileOutputStream fos=null;
InputStream imageDataIS =null;
try {
//用于页面显示拍卖品图片
Blob imageData = g.getAuctionpic();
//拍卖品图片生成名称
String picName=g.getAuctionid()+"."+g.getAuctionpictype();
///tempFile是生成的拍卖品图片存放路径
String file=this.getServletContext().getRealPath("/tempFile")+"/"+picName;
imageDataIS = imageData.getBinaryStream();
fos = new FileOutputStream(file);
byte[] bt=new byte[1024];
while(imageDataIS.read(bt)!=-1){
fos.write(bt);
fos.flush();
}
g.setPicName(picName);
request.getSession().setAttribute("onegood",g);
List<Auctionrecord> list=abi.findByAuctio(g);
request.getSession().setAttribute("state",i);
request.getSession().setAttribute("usergood",list);
request.getRequestDispatcher("auctiongood.jsp").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
request.setAttribute("message", e.getMessage());
request.getRequestDispatcher("error.jsp").forward(request,response);
}finally{
if(fos!=null)
fos.close();
if(imageDataIS!=null)
imageDataIS.close();
}
}
jsp显示图片============================================================
<div class="rg borders"><img src="tempFile/${onegood.picName }" width="270" height="185" alt="" /></div>
客户端显示之后的结果:<img src="tempFile/6.jpg" width="270" height="185" alt="" />
============================================================
============================================================
============================================================
============================================================
struts2实现图片的上传以及下载功能
最新推荐文章于 2021-04-06 13:48:58 发布