关于jspsmartupload的上传

我自己做的一个项目里的一部分。

提交表单的jsp页面:

//首句
<% @ page contentType = " text/html; charset=gb2312 "  language = " java "   import = " java.sql.* "  errorPage = ""   %>
//body的form,提交到servlet
< form id = " form1 "  name = " form1 "  method = " post "  action = " /news/servlet "  enctype = " multipart/form-data " >
//一个文本框
   
< p align = " center " >
        
< label > 二级标题:
        
< input name = " txtSecond "  type = " text "  id = " txtSecond "   />
        
</ label >
      
</ p >
//一个file框
  
< p align = " center " >
        
< label >  文件位置:  </ label >
        
< input type = " file "  name = " txtFile "   />
  
</ p >
      
< p align = " center " >
        
< input type = " submit "  name = " Submit "  value = " 提交 " />
        
< input name = " Reset "  type = " reset "  id = " Reset "  value = " 重置 "   />
      
</ p >
</ form >

用来接收的servlet:

package  news;

import  javax.servlet. * ;
import  javax.servlet.http. * ;
import  java.sql. * ;
import  java.io. * ;
import  java.util. * ;
import  java.text. * ;
// 导入jspsmart包
import  com.jspsmart.upload. * ;

public   class  SecondLevelTitlePublishServlet  extends  HttpServlet  {
    
public String titleName=null;
    
public int parentId=0;
    
private ServletConfig config;
    
private static final String CONTENT_TYPE = "text/html; charset=gb2312";

    
//Initialize global variables
    final public void init(ServletConfig config) throws ServletException {
        
this.config = config;
    }


    
final public ServletConfig getServletConfig() {
        
return config;
    }


    
//Process the HTTP Post request
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException 
{
        response.setContentType(CONTENT_TYPE);
        PrintWriter out 
= response.getWriter();
        
try {
             
//上传初始化
             SmartUpload upload = new SmartUpload();
             upload.initialize(config, request, response);
             upload.upload();
             
//获取second.jsp里面的text值
             String secondTitle = upload.getRequest().getParameter("txtSecond");
             com.jspsmart.upload.File myFile 
= upload.getFiles().getFile(0);
             String fileName 
= myFile.getFileName();
             
if (secondTitle != null && myFile != null{
                
if (!myFile.isMissing()) {
                myFile.saveAs(
"E:/inews/upload/" + fileName,upload.SAVE_PHYSICAL);
                
//得到当前时间
                SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd-" + " " + "hh:mm:ss");
                String datetime 
= tempDate.format(new java.util.Date());
                
//l连接数据库
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection conn
=DriverManager.getConnection("jdbc:odbc:newsServer","sa","");
                String sql
="insert into SecondLevelTitle(TitleName,FilePath,CreatTime) values('"+fileName+"','"+"E:/inews/upload/" + fileName+"','"+datetime+"')";
                Statement sta
=conn.createStatement();
                sta.executeUpdate(sql);
                out.println(
"<script>alert("发布成功!");</script>");
                out.println(
"<script>window.location="/news/html/right.html";</script>");
                sta.close();
                conn.close();
             }

             
else {
                out.println(
"<script>alert("没有找到文件!");history.go(-1);</script>");
             }

           }

           
catch (Exception e) {
               System.out.println(e);
           }

           out.close();
     }

      
    
//Clean up resources
    public void destroy() {
    }

}

再从另外的jsp页面读取文件内容到jsp页面上:

<%
// 此ID为上一个页面url传过来的ID: http://localhost :8080/news/html/index.jsp?secondId=1。因为ID在数据库中是自变量,
// 所以在前面的插入当中不用写插入。
int  secondId = Integer.parseInt(request.getParameter( " secondId " ));
try {
  Class.forName(
"sun.jdbc.odbc.JdbcOdbcDriver");
  Connection conn
=DriverManager.getConnection("jdbc:odbc:newsServer","sa","");
  String sql
="select filePath from SecondLevelTitle where Id='"+secondId+"'";
  java.sql.Statement sta
=conn.createStatement();
  ResultSet rs
=sta.executeQuery(sql);
  String filePath
=null;
  
while(rs.next()){
    filePath
=rs.getString("FilePath");
  }
  
  File file
=new File(filePath);
  
char[] charBuffer=new char[10];
  
int length=0;
  
if(file.exists()){
    FileReader fileReader
=new FileReader(file);
    
while((length=fileReader.read(charBuffer))!=-1){
      out.write(charBuffer,
0,length);
    }

    fileReader.close();
  }

  
else{
    System.out.println(
"没有找到相关文件!");
  }

  rs.close();
  sta.close();
  conn.close();
}

catch (Exception e) {
  System.out.println(e);
}

%>

 

因为是从整个项目的代码里面分割出来的,也许有地方会分割错了,还请大家帮忙在意下。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值