利用uploadbean实现上传

程序代码

<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8"%>
<%@ page import="com.fredck.FCK editor.*" %>
<%@ page import="com.lineteam.hibernate.news.entity.*" %>
<%@ page import="com.lineteam.hibernate.DAO.*"%>
<%@ taglib uri="/WEB-INF/fck editor.tld" prefix="fck" %>

<html>
<head>
<title>图片添加</title>
     <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
<script type="text/javascript" src="../FCK editor/fck editor.js"></script>  
<link href="css/main.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="white">
<%
ISearch search=Search.getInstance();
String hql="from PicType n order by n.id desc";
List list=search.search(hql);
Iterator it=list.iterator();
%> 
  <table width="800" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td align="center" valign="top">
      <div id="list2">
<table width="700" border="0" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC">
<tbody>
<form action="upphoto. jspenctype="multipart/form-data" method="post" name="form1">
  <tr>
      <td height="42" colspan="3" align="center" bgcolor="#E7E7E7" class="title">图片添加</td>
    </tr>
  <tr>
    <td height="33" align="center" bgcolor="#FFFFFF">图片分类:</td>
    <td align="left" bgcolor="#FFFFFF">
    
    <select name="pictype">
        <%
       while(it.hasNext())
       {
       PicType pictype=(PicType)it.next();
       %>
       <option value="<%=pictype.getId()%>"><%=pictype.getTypeName()%></option>
       <%
       }
       %>
    </select>
    </td>
    <td align="left" bgcolor="#FFFFFF" ><span class="redsign">* </span>请选择图片分类</td>
  </tr>
  <tr>
    <td width="20%" height="33" align="center" bgcolor="#FFFFFF">图片标题:</td>
    <td width="51%" align="left" bgcolor="#FFFFFF"><input name="title" type="text" size="50" id="title"></td>
    <td width="29%" align="left" bgcolor="#FFFFFF" >可选,如果有新闻则为必填项</td>
  </tr>
  <tr>
    <td height="33" align="center" bgcolor="#FFFFFF">上传图片:</td>
    <td align="left" bgcolor="#FFFFFF" class="selelist"><input name="upfile" type="file" size="38"></td>
    <td align="left" bgcolor="#FFFFFF" class="redsign"><span class="redsign">*</span> 请选择上传的图片</td>
  </tr>
  
  
  <tr>
    <td align="center" bgcolor="#FFFFFF">文章内容:</td>
    <td height="520" colspan="2" align="left" valign="middle" bgcolor="#FFFFFF">
    <fck: editor id="content" basePath="/edu/FCK editor/" width="580" height="520" skinPath="/edu/FCK editor/ editor/skins/silver/"  
   toolbarSet = "Default">
   </fck: editor></td>
    </tr>
  <tr>
    <td height="40" colspan="3" align="center" bgcolor="#FFFFFF">
    <input type="submit" name="savenew" value="保存图片"> 
      <input type="reset" name="Submit2" value="重置"></td>
    </tr>
  </form>
  </tbody>
</table>
</div>
 </td>
</tr>
</table>
<br>

</body>
</html>



程序代码 程序代码

<%@ page contentType="text/html;charset=utf-8" language="java" import="java.util.*"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="com.lineteam. jspbean.manager.PhotoAdmin"%>
<%@ page import="javazoom.upload.*"%>
<%@ page import="java.io.File"%>
<%@ page import="uploadutilities.FileMover"%>
<%
String pPath = request.getRealPath("/");
String sPath=pPath+"newadmin//upimages";
%>
< jsp:useBean id="photo" scope="page" class="com.lineteam. jspbean.manager.PhotoAdmin" />
< jsp:useBean id="upBean" scope="page" class="javazoom.upload.UploadBean" >
  < jsp:setProperty name="upBean" property="folderstore" value="<%=sPath %>" />
</ jsp:useBean>

<%
String okMessage="";
String oklink="";
String  errorMessage="";
String returnwhere="";
String type="";
String fpath="";
String id="";
String content="";
String title="";
long fsize=0;
PhotoAdmin picbean=new PhotoAdmin();
int x=0;
try{
    request.setCharacterEncoding("UTF-8");
   FileMover fileMover = new FileMover();
   MultipartFormDataRequest mrequest = null;
   Hashtable files = null;
   
   if (MultipartFormDataRequest.isMultipartFormData(request))
      {
      mrequest = new MultipartFormDataRequest(request,null,100*1024*1024,MultipartFormDataRequest.COSPARSER,"UTF-8");//注意这里也要设置编码参数
     title = mrequest.getParameter("title");  //获得图片标题
     id= mrequest.getParameter("pictype");   //获得图片类型
     content=mrequest.getParameter("content");  //获得图片描述
     files = mrequest.getFiles();
   }
 int iFileCount = 0;
 String sServerFileName="";
 String sLocalFileName = "";
 //文件获取
 if ( (files != null) || (!files.isEmpty()) ) {
 
  iFileCount = files.size();
  UploadFile file = (UploadFile) files.get("upfile");
  sLocalFileName=file.getFileName();
  fsize=file.getFileSize();
  out.println("sLocalFileName:"+sLocalFileName);
  int ii= sLocalFileName.indexOf("."); // 取文件名的后缀
  String sExt = sLocalFileName.substring(ii,sLocalFileName.length());
  // 得到不重复的文件名
  Date dt = new java.util.Date(System.currentTimeMillis());
  SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
  sServerFileName= fmt.format(dt);
  sServerFileName =sServerFileName + sExt;
  //如果不存在该目录,则新建一个
  File dir =new File(sPath);
  if (!dir.exists())
  {
   dir.mkdirs();
  }
   upBean.addUploadListener(fileMover);//增加filMover监听
   fileMover.setNewfilename(sServerFileName);//设置服务器上的文件名
   upBean.store(mrequest,"upfile");//上传
  fpath="upimages/"+sServerFileName;
 }
  photo.savePhoto(id,title,fpath,content,(String.valueOf(fsize)));  //save 
  if(picbean.getErrorMessage().equals(""))
        okMessage="恭喜您,图片添加成功!";
    else
        okMessage=picbean.getErrorMessage();
        oklink="addphoto. jsp";
        x=-1;
        %><%@ include file="../include/ok.inc" %>
        <%
  
}catch(Exception e)
{
    out.println(e.getMessage());
     errorMessage=picbean.getErrorMessage();%>

    <%@ include file="../include/err.inc" %>
    <%
    return;
}

 %>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值