[size=large][color=blue][b]jsp中的文件上传例子[/b][/color][/size]
package org.svse.action;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class FileUpServlet extends HttpServlet {
private ServletConfig config;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("gbk");
PrintWriter out = response.getWriter();
int ncount = 0;//记录上传文件数
// 声明一个上传组件对象
SmartUpload su = new SmartUpload();
// 初始化
su.initialize(config, request, response);
//设置上传文件的最大3mb
su.setTotalMaxFileSize(3*1024*1024);
// 上传文件到服务器
try {
su.upload();
String uploadfile = request.getRealPath("upload");//得到文件存放的绝对路径
File upfile = new File(uploadfile);
if(!upfile.exists()){
upfile.mkdir();//如果不存在就要创建它
}
ncount = su.save("/upload");
out.println(ncount + "file(s) uploaded");
} catch (SmartUploadException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
}
[size=large][color=blue][b]jsp文件下载[/b][/color][/size]
package org.svse.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class FileDownServlet extends HttpServlet {
private ServletConfig config;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
SmartUpload su = new SmartUpload();//实例化组件
su.initialize(config, request, response);//初始化
try {
su.downloadFile("upload/new.txt");//这个文件必须在你工程的upload目录中存在(现
在还是固定的)
} catch (SmartUploadException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
}
[size=large][color=blue][b]jsp界面
[/b][/color][/size]
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="FileUpServlet" method="post" enctype="multipart/form-data" >
请选择你要上传的文件:<input type="file" name="file1" /><br />
<input type="submit" value=" 上传 ">
</form>
<a href="FileDownServlet">下载文件</a>
</body>
</html>
注意:在使用smart工具上传文件时,表单的提交方式method="post" ,编码enctype="multipart/form-data"。在下面的附件中提供了完整的例子和smart工具的jar包(支持中文文件名的上传)
package org.svse.action;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class FileUpServlet extends HttpServlet {
private ServletConfig config;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("gbk");
PrintWriter out = response.getWriter();
int ncount = 0;//记录上传文件数
// 声明一个上传组件对象
SmartUpload su = new SmartUpload();
// 初始化
su.initialize(config, request, response);
//设置上传文件的最大3mb
su.setTotalMaxFileSize(3*1024*1024);
// 上传文件到服务器
try {
su.upload();
String uploadfile = request.getRealPath("upload");//得到文件存放的绝对路径
File upfile = new File(uploadfile);
if(!upfile.exists()){
upfile.mkdir();//如果不存在就要创建它
}
ncount = su.save("/upload");
out.println(ncount + "file(s) uploaded");
} catch (SmartUploadException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
}
[size=large][color=blue][b]jsp文件下载[/b][/color][/size]
package org.svse.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class FileDownServlet extends HttpServlet {
private ServletConfig config;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
SmartUpload su = new SmartUpload();//实例化组件
su.initialize(config, request, response);//初始化
try {
su.downloadFile("upload/new.txt");//这个文件必须在你工程的upload目录中存在(现
在还是固定的)
} catch (SmartUploadException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
}
[size=large][color=blue][b]jsp界面
[/b][/color][/size]
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="FileUpServlet" method="post" enctype="multipart/form-data" >
请选择你要上传的文件:<input type="file" name="file1" /><br />
<input type="submit" value=" 上传 ">
</form>
<a href="FileDownServlet">下载文件</a>
</body>
</html>
注意:在使用smart工具上传文件时,表单的提交方式method="post" ,编码enctype="multipart/form-data"。在下面的附件中提供了完整的例子和smart工具的jar包(支持中文文件名的上传)