利用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
    评论
实现在 JSP 页面中上文字和图片,需要涉及到以下几个步骤: 1. 编写 JSP 页面,设置表单,包含文字和图片上控件。 2. 在 JSP 页面中,使用 JavaBean 技术获取表单提交的数据,包括文字和图片。 3. 将获取到的数据插入到 SQL Server 数据库中,包括文字和图片。 下面是一个简单的实现示例: 1. 编写 JSP 页面 ```jsp <form action="upload.jsp" method="post" enctype="multipart/form-data"> <label for="title">标题:</label> <input type="text" id="title" name="title"><br> <label for="content">内容:</label> <textarea id="content" name="content"></textarea><br> <label for="image">图片:</label> <input type="file" id="image" name="image"><br> <input type="submit" value="提交"> </form> ``` 2. 编写 JavaBean ```java public class UploadBean { private String title; private String content; private String imagePath; public UploadBean(HttpServletRequest request) throws IOException, ServletException { title = request.getParameter("title"); content = request.getParameter("content"); Part imagePart = request.getPart("image"); if (imagePart != null) { String imageName = getSubmittedFileName(imagePart); imagePath = "/uploads/" + imageName; String realPath = request.getServletContext().getRealPath("/uploads"); File imageFile = new File(realPath, imageName); try (InputStream is = imagePart.getInputStream(); OutputStream os = new FileOutputStream(imageFile)) { byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) != -1) { os.write(buffer, 0, length); } } } } public void saveToDatabase() throws SQLException, ClassNotFoundException { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String url = "jdbc:sqlserver://localhost:1433;databaseName=mydatabase"; String username = "myuser"; String password = "mypassword"; try (Connection conn = DriverManager.getConnection(url, username, password)) { String sql = "INSERT INTO mytable (title, content, image_path) VALUES (?, ?, ?)"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, title); stmt.setString(2, content); stmt.setString(3, imagePath); stmt.executeUpdate(); } } } private String getSubmittedFileName(Part part) { String header = part.getHeader("content-disposition"); if (header == null) { return null; } for (String token : header.split(";")) { if (token.trim().startsWith("filename")) { return token.substring(token.indexOf('=') + 1).trim().replace("\"", ""); } } return null; } } ``` 3. 编写 JSP 页面处理上操作 ```jsp <%@ page import="java.io.*, java.sql.*" %> <%@ page import="javax.servlet.http.*, javax.servlet.*" %> <% try { UploadBean uploadBean = new UploadBean(request); uploadBean.saveToDatabase(); out.println("上成功!"); } catch (Exception e) { out.println("上失败:" + e.getMessage()); } %> ``` 在这个示例中,我们使用 JavaBean 技术获取表单提交的数据,并将图片保存到服务器上的指定目录中。然后,我们使用 SQL Server JDBC 驱动程序将数据插入到数据库中。最后,在 JSP 页面中展示上结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值