首页 新闻 会员 周边

学习之用 迫切需要

0
悬赏园豆:10 [待解决问题]

哪位高人可以给我发点基于ssh的文件上传和下载的代码和解释  谢谢 ~  仅仅是学习之用

文博_king的主页 文博_king | 初学一级 | 园豆:192
提问于:2012-07-16 09:51
< >
分享
所有回答(2)
0

http://www.cnblogs.com/mbailing/archive/2010/10/26/1861712.html

这里有一些资料,希望对你有用。

jerry-Tom | 园豆:4077 (老鸟四级) | 2012-07-16 10:35

恩 谢谢啊  不过我要的是jsp方面的

支持(0) 反对(0) 文博_king | 园豆:192 (初学一级) | 2012-07-16 10:39
0
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding(
"GBK");
request.setCharacterEncoding(
"GBK");
response.setContentType(
"text/html");
String URL
="jdbc:mysql://localhost:3306/test?user=root&"
+"password=admin&useUnicode=true&characterEncoding=gbk";
Connection conn
=null;
Statement stmt
=null;
// 判断提交过来的表单是否为文件上传表单
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
String name
=null;
String fileName
=null;
String path
=null;
if (isMultipart) {
// 构造文件上传处理对象
FileItemFactory factory =new DiskFileItemFactory();
ServletFileUpload upload
=new ServletFileUpload(factory);
Iterator items;
try {
// 解析出表单中提交的所有文件内容
items = upload.parseRequest(request).iterator();
while (items.hasNext()) {
FileItem item
= (FileItem) items.next();
name
= item.getName();
// 获取文件名
fileName = name.substring(name.lastIndexOf('\\') +1, name
.length());
// 获取文件上传后的存储路径(上传后的文件将存储到当前应用项目的file文件夹中)
// File.separatorChar相当于/
String realPath = request.getRealPath("upload");
path
= realPath + File.separator + fileName;
File uploadFile
=new File(path);
item.write(uploadFile);
String sql
="insert into file(filename,filepath) value('"+fileName+"','"+path+"')";
Class.forName(
"com.mysql.jdbc.Driver");
conn
= DriverManager.getConnection(URL);
stmt
= conn.createStatement();
stmt.execute(sql);
PrintWriter out
= response.getWriter();
out.println(
"<p align='center'><font size='2'>上传的文件为:"
+ name +"<br>");
out.println(
"<font size='2'>保存的路径是:"+ path +"</p>");
}

}
catch (Exception e) {
}
}
}
Tom.汤 | 园豆:3028 (老鸟四级) | 2012-07-16 12:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册