JSP上传文件轻松实现
现在jsp(SUN企业级应用的首选)被越来越多的人应用在后台程序的开发及动态网页的制作上了,今天就来介绍一下如何用jsp(SUN企业级应用的首选)来实现文件的上传。
搞了几天,在网上查了不少资料,终于把上传功能解决了。程序如下:
package linkman.helper;
import java.io.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletInputStream;
public class UpLoadBean {
private String SavePath = null;
private String FileName = null;
private String Sperator = null;
private String ErrMsg = "正常返回";
private byte[] bt = new byte[4096];
private byte[] prebt = new byte[4096];
private int t = -1;
private long FileSize = -1;
private boolean UserDefineFileName = false;
/////////////////////////////////////////////////
public UpLoadBean () {
FileName = bringFileName ();
}
public UpLoadBean (String savepath) {
SavePath = savepath;
FileName = bringFileName ();
}
/////////////////////////////////////////////////
// 文件名信息
private String bringFileName () {
Calendar dt = Calendar.getInstance ();
String str = "" + dt.get (Calendar.YEAR) + dt.get (Calendar.MONTH) + dt.get (Calendar.DAY_OF_MONTH);
str = str + dt.get (Calendar.HOUR) + dt.get (Calendar.MINUTE) + dt.get (Calendar.SECOND);
return str;
}
public void setFileName (String filename) {
FileName = filename;
UserDefineFileName = true;
}
public String getFileName () {
return FileName;
}
/////////////////////////////////////////////////
// 上传文件主程序
public boolean UpLoad (HttpServletRequest request) throws Exception {
if (SavePath == null) {
setErrorMessage ("未设置保存上传文件的路径");
return false;
}
request.setCharacterEncoding("utf-8");
ServletInputStream in = request.getInputStream ();
////////////////////////////////////////////
// 获取得一行的分隔标志
t = in.readLine (bt, 0, bt.length);
if (t != -1) {
Sperator = new String (bt, 0, t);
Sperator = Sperator.substring (0, 28);
t = -1;
}
////////////////////////////////////////////
// 取扩展名并设定文件全名
do {
t = in.readLine (bt, 0, bt.length);
String s = new String (bt, 0, t);
int index = s.indexOf ("filename="");
if (index != -1) {
&nbs
相关新闻>>
- 发表评论
-
- 最新评论 更多>>