您的位置:首页 > 其它

用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文件包
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: