用cos文件上传
2011-08-01 23:19
239 查看
========================jsp页面=============================
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<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="executeUpload.jsp" method="post" enctype="multipart/form-data">
your name:<input type="text" name="name"/><br/>
your file:<input type="file" name="fileName"/><br/>
上传:<input type="submit" value="上传">
</form>
</body>
</html>
=======================上传处理=========================
<%
//使用cos上传文件
//设置post的最大字节是10M,该类用于解析HTTP请求
MultipartParser mp=new MultipartParser(request,10*1024*1024);
//所有表单域都是part的实例,他有两个子类1:ParamPart普通表单域,2:FilePart文件表单域
Part part;
//遍历请求中的所有表单
while((part=mp.readNextPart())!=null){
//取得表单的name属性
String name=part.getName();
//表单域是普通表单域
if(part.isParam()){
//取得表单域的值
ParamPart parampart=(ParamPart)part;
String value=parampart.getStringValue("GBK");
out.print("扑通表单域的name:"+name+"<br/>value:"+value);
}//表单域是文件表单域
else if(part.isFile()){
FilePart filePart=(FilePart)part;
String fileName=filePart.getFileName();
if(fileName!=null){
File file=new File(request.getRealPath("/")+"userFile");
if(!file.exists()){
file.mkdirs();
}
//上传文件
long size=filePart.writeTo(file);
out.println("文件大小:"+size);
out.println("上传文件的内容"+filePart.getFilePath());
out.println("上传文件的类型:"+filePart.getContentType());
}else{
//文件是空的
out.print("上传文件为空的");
}
out.flush();
}
}
%>
===============:注意 用cos上传的时候需要她想要的jar包:cos.jar文件包
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<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="executeUpload.jsp" method="post" enctype="multipart/form-data">
your name:<input type="text" name="name"/><br/>
your file:<input type="file" name="fileName"/><br/>
上传:<input type="submit" value="上传">
</form>
</body>
</html>
=======================上传处理=========================
<%
//使用cos上传文件
//设置post的最大字节是10M,该类用于解析HTTP请求
MultipartParser mp=new MultipartParser(request,10*1024*1024);
//所有表单域都是part的实例,他有两个子类1:ParamPart普通表单域,2:FilePart文件表单域
Part part;
//遍历请求中的所有表单
while((part=mp.readNextPart())!=null){
//取得表单的name属性
String name=part.getName();
//表单域是普通表单域
if(part.isParam()){
//取得表单域的值
ParamPart parampart=(ParamPart)part;
String value=parampart.getStringValue("GBK");
out.print("扑通表单域的name:"+name+"<br/>value:"+value);
}//表单域是文件表单域
else if(part.isFile()){
FilePart filePart=(FilePart)part;
String fileName=filePart.getFileName();
if(fileName!=null){
File file=new File(request.getRealPath("/")+"userFile");
if(!file.exists()){
file.mkdirs();
}
//上传文件
long size=filePart.writeTo(file);
out.println("文件大小:"+size);
out.println("上传文件的内容"+filePart.getFilePath());
out.println("上传文件的类型:"+filePart.getContentType());
}else{
//文件是空的
out.print("上传文件为空的");
}
out.flush();
}
}
%>
===============:注意 用cos上传的时候需要她想要的jar包:cos.jar文件包
相关文章推荐
- 腾讯云cos上传核心文件调用简单注释
- 用cos上传文件
- jsp中用cos上传文件并重命名
- 用O’Reilly公司的cos实现文件上传
- COS文件上传和下载
- cos ,FileUpload ,SmartUpload 上传文件性能比较
- 用cos进行文件上传【转】
- 使用COS组件实现文件上传
- cos实现文件上传--推荐
- jsp两个不同组件实现文件上传总结--cos.jar包和commons-fileupload.jar包
- cos与EXT调用AJAX提交上传文件
- JSP+Servlet中使用cos.jar进行图片上传(文件上传亦然)
- struts文件上传、Cos组件文件上传
- fileupload和cos文件上传技术区别
- jsp中利用cos进行web文件上传
- 【COS上传文件】实现文件重命名
- 用cos进行文件上传
- JSP+Servlet中使用cos.jar进行图片上传(文件上传亦然)
- cos上传文件
- 使用COS上传文件(转载)