最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

JSP上传文件

来源:动视网 责编:小OO 时间:2025-09-29 05:23:17
文档

JSP上传文件

UploadExample.jsp 上传文件程序应用示例请选择要上传的文件 来源:(http://blog.sina.com.cn/s/blog_4cc16fc50100bw3n.html)-用JSP实现文件上传功能_chuansir_新浪博客doUpload.jsp upFile=0){//读入上传的数据in=newDataInputStream(request.getInputStream());intformDataLength=request.getContentLength();if
推荐度:
导读UploadExample.jsp 上传文件程序应用示例请选择要上传的文件 来源:(http://blog.sina.com.cn/s/blog_4cc16fc50100bw3n.html)-用JSP实现文件上传功能_chuansir_新浪博客doUpload.jsp upFile=0){//读入上传的数据in=newDataInputStream(request.getInputStream());intformDataLength=request.getContentLength();if
UploadExample.jsp

 

<%@ page contentType="text/html;charset=gb2312"%>

<%= application.getServerInfo() %>

上传文件程序应用示例

<%-- 类型enctype用multipart/form-data,这样可以把文件中的数据作为流式数据上传,不管是什么文件类型,均可上传。--%>

请选择要上传的文件

 

来源:(http://blog.sina.com.cn/s/blog_4cc16fc50100bw3n.html) - 用JSP实现文件上传功能_chuansir_新浪博客 

doUpload.jsp

 

<%@ page contentType="text/html; charset=GBK" %>

<%@ page import="java.io.*"%>

<%@ page import="java.util.*"%>

<%@ page import="javax.servlet.*"%>

<%@ page import="javax.servlet.http.*"%>

upFile

<%

//定义上载文件的最大字节

int MAX_SIZE = 102400 * 102400;

// 创建根路径的保存变量

String rootPath;

//声明文件读入类

DataInputStream in = null;

FileOutputStream fileOut = null;

//取得客户端的网络地址

String remoteAddr = request.getRemoteAddr();

//获得服务器的名字

String serverName = request.getServerName();

//取得互联网程序的绝对地址

String realPath = request.getRealPath(serverName);

realPath = realPath.substring(0,realPath.lastIndexOf("\\\\"));

//创建文件的保存目录

rootPath = realPath + "\\\d\\\";

//取得客户端上传的数据类型

String contentType = request.getContentType();

try{

if(contentType.indexOf("multipart/form-data") >= 0){

//读入上传的数据

in = new DataInputStream(request.getInputStream());

int formDataLength = request.getContentLength();

if(formDataLength > MAX_SIZE){

out.println("

上传的文件字节数不可以超过" + MAX_SIZE + "

");

return;

}

//保存上传文件的数据

byte dataBytes[] = new byte[formDataLength];

int byteRead = 0;

int totalBytesRead = 0;

//上传的数据保存在byte数组

while(totalBytesRead < formDataLength){

byteRead = in.read(dataBytes,totalBytesRead,formDataLength);

totalBytesRead += byteRead;

}

//根据byte数组创建字符串

String file = new String(dataBytes);

//out.println(file);

//取得上传的数据的文件名

String saveFile = file.substring(file.indexOf("filename=\\"") + 10);

saveFile = saveFile.substring(0,saveFile.indexOf("\\n"));

saveFile = saveFile.substring(saveFile.lastIndexOf("\\\\") + 1,saveFile.indexOf("\\""));

int lastIndex = contentType.lastIndexOf("=");

//取得数据的分隔字符串

String boundary = contentType.substring(lastIndex + 1,contentType.length());

//创建保存路径的文件名

String fileName = rootPath + saveFile;

//out.print(fileName);

int pos;

pos = file.indexOf("filename=\\"");

pos = file.indexOf("\\n",pos) + 1;

pos = file.indexOf("\\n",pos) + 1;

pos = file.indexOf("\\n",pos) + 1;

int boundaryLocation = file.indexOf(boundary,pos) - 4;

//out.println(boundaryLocation);

//取得文件数据的开始的位置

int startPos = ((file.substring(0,pos)).getBytes()).length;

//out.println(startPos);

//取得文件数据的结束的位置

int endPos = ((file.substring(0,boundaryLocation)).getBytes()).length;

//out.println(endPos);

//检查上载文件是否存在

File checkFile = new File(fileName);

if(checkFile.exists()){

out.println("

" + saveFile + "文件已经存在.

");

}

//检查上载文件的目录是否存在

File fileDir = new File(rootPath);

if(!fileDir.exists()){

fileDir.mkdirs();

}

//创建文件的写出类

fileOut = new FileOutputStream(fileName);

//保存文件的数据

fileOut.write(dataBytes,startPos,(endPos - startPos));

fileOut.close();

out.println(saveFile + "文件成功上载.

");

}else{

String content = request.getContentType();

out.println("

上传的数据类型不是multipart/form-data

");

}

}catch(Exception ex){

throw new ServletException(ex.getMessage());

}

%>

文档

JSP上传文件

UploadExample.jsp 上传文件程序应用示例请选择要上传的文件 来源:(http://blog.sina.com.cn/s/blog_4cc16fc50100bw3n.html)-用JSP实现文件上传功能_chuansir_新浪博客doUpload.jsp upFile=0){//读入上传的数据in=newDataInputStream(request.getInputStream());intformDataLength=request.getContentLength();if
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top