JSP实现图片上传代码

JSP实现图片上传代码
<title>upload.jsp</title>
<%
String path = request.getContextPath();
%>
  <body>
<html:errors/>
  <form action="upld.do" method="post">
  <img id="image" name="image" src="<%=path %>/W.JPG" width="100"/>
  <input id="file" type="file" name="file" οnchange="setImage()"/>
  <input type="submit" value="上传">
  </form>
  <script language="javascript">
     function setImage()
     {
        document.getElementById("image").src = document.getElementById("file").value;
     }
</script>
  
  </body>
<title>img.jsp</title>
<%
HttpSession se = request.getSession();
Integer m = Integer.parseInt(se.getAttribute("m").toString());
%>
  <body>
    <%
     String driverName = "com.mysql.jdbc.Driver";
     String url = "jdbc:mysql://localhost:3306/user";
     String username = "root";
     String password = "123456";
     Connection con;
     PreparedStatement pst = null;
     ResultSet rs;
     byte bu[] = new byte[m];
     Class.forName(driverName);
     con = DriverManager.getConnection(url, username, password);
     pst = con.prepareStatement("select * from img ");
     rs = pst.executeQuery();
     while (rs.next()) {
      //response.reset();
      InputStream in2 = rs.getBinaryStream("name");
      in2.read(bu);
      response.setContentType("image/jpeg");
      in2.close();
     }
      response.getOutputStream().write(bu);
      
     con.close();
      %>
  </body>
<title>error.jsp</title>
<%
String path=request.getContextPath();
%>
  <body>
      <script language="javascript">
          alert("上传文件太大!");
          document.location.replace("<%=path%>/upload.jsp");
      </script>
  </body>
<title>uploadsuc.jsp</title>
<%
    String path=request.getContextPath();
%>
  <body>
    <script type="text/javascript">
        alert("存入数据库成功!");
        document.location.href("<%=path%>/img.jsp");
    </script>
  </body>
<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="upldForm" type="mstruts.form.UpldForm" />
  </form-beans>
  <global-exceptions />
  <global-forwards >
   <forward name="up" path="/img.jsp"></forward>
    <forward name="error" path="/error.jsp"></forward>
    <forward name="uploadsuc" path="/uploadsuc.jsp"></forward>
  </global-forwards>
  <action-mappings >
    <action
      input="/upload.jsp"
      name="upldForm"
      path="/upld"
      scope="request"
      type="mstruts.action.UpldAction" >
      
      </action>
    
  </action-mappings>
  <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>
ApplicationResources.properties
fileerrors=<li>/u8bf7/u6d4f/u89c8/u4e0a/u4f20/u6587/u4ef6/uff01
UpldForm.java
public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {
  // TODO Auto-generated method stub
  ActionErrors errors=new ActionErrors();
  if(request.getParameter("file")=="")
  {
   errors.add("file",new ActionMessage("fileerrors"));
  }
  return errors;
}
UpldAction.java
public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  UpldForm upldForm = (UpldForm) form;// TODO Auto-generated method stub
  try {
   String path = new String(request.getParameter("file").getBytes("iso-8859-1"),"gb2312");
  String driverName="com.mysql.jdbc.Driver";
  String url="jdbc:mysql://localhost:3306/user";
  String username="root";
  String password="123456";
  Connection con;
        PreparedStatement pst=null;
         try {
     Class.forName(driverName);
     con=DriverManager.getConnection(url,username,password);
                  FileInputStream in=new FileInputStream(path);
                  int m=in.available();//字节数量
                  if(m>100*1024)
                  {
                   return mapping.findForward("error");  
                  }
                  else
                  {
                  HttpSession se=request.getSession();
                  se.setAttribute("m", m);
                  pst=con.prepareStatement("insert into img(name) value(?)") ;
                  
                  pst.setBinaryStream(1,in,in.available());    
                  pst.executeUpdate();                  
                  in.close();  
                  con.close();
                  return mapping.findForward("uploadsuc");
                  }
    } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
  } catch (UnsupportedEncodingException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }  
  
  return null;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值