关于jspsmartupload的上传

来源:互联网 发布:sql from unixtime 编辑:程序博客网 时间:2024/06/06 01:18

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

提交表单的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);
}

%>

 

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

原创粉丝点击