关于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
//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>
<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>
<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() ...{
}
}
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);
}
%>
//此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);
}
%>
因为是从整个项目的代码里面分割出来的,也许有地方会分割错了,还请大家帮忙在意下。
- 关于jspsmartupload的上传
- 关于JspSmartUpload上传文件大小限制的问题
- 关于jspsmartupload中文名文件上传后为乱码的问题
- 关于使用jspsmartupload.jar进行文件的上传与下载
- jspsmartupload上传和其他的上传
- jspsmartupload上传
- 关于用jspSmartUpload控件上传文件并附带参数的吐血体验
- jspSmartUpload对上传的文件进行删除
- 基于jspSmartUpload的Jsp文件上传实例
- 使用jspSmartUpload组件实现图片的上传
- jspSmartUpload实现图片的批量上传
- 使用jspSmartUpload实现文件的上传
- 采用jspSmartUpload组件进行文件的上传
- jspSmartUpload上传图片的路径问题
- [JSP]jspsmartupload完成简单的文件上传
- 关于用jspSmartUpload控件上传文件并附带参数
- 关于jspsmartupload 中文各种乱码的解决方法
- jspSmartUpload上传组件
- 确定用户运行的是哪个Windows版本,以及如何验证?
- 利用ajaxpro.dll进行Ajax的开发
- Internet信息获取能力
- 缩小客户区(扩大非客户区)的方法
- DataList绑定的方法
- 关于jspsmartupload的上传
- 终于在csdn上安家了!这个博客将作为我学习java历程,记录我心得的一个地方
- C#使用ICSharpCode.SharpZipLib压缩文件
- 遍历文件夹中指定文件
- MYSQL操作类
- 个别打印
- window.showModelessDialog()
- test图片
- ADO访问数据库.