cos上传文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>enctype属性测试</title>
</head>
<body>
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="upload2.jsp">
请求参数 <input type="text" name="type" /><br>
上传文件: <input type="file" name="file" /><br>
上传文件: <input type="file" name="file2" /><br>
上传文件: <input type="file" name="file3" /><br>
<input name="dd" type="submit" value="提交" />
</form>
</body>
</html>
<%@ page contentType="text/html;charset=GBK" import="java.io.*,java.util.*"%>
<%@ page import="com.oreilly.servlet.multipart.*,com.oreilly.servlet.*"%>
<%@ page import="com.smsgov2.dy.sql.*"%>
<%
//设置POST请求的内容最大字节为10M,该类用于解析HTTP请求
MultipartParser mp = new MultipartParser(request, 10*1024*1024);
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.1.2:8796/test?useUnicode=true&characterEncoding=gb2312";
String user = "winteam";
String pass = "024318062557255";
String type = "";
MyDataBaseManager_2 mdbm = new MyDataBaseManager_2(driver,url,user,pass);
String saveDirectory ="e:\\upload";
//所有表单域都是Part实例
Part part;
//遍历请求中的所有表单域
while ((part = mp.readNextPart()) != null){
//取得表单域的name属性值
String name = part.getName();
//对于普通表单域
if (part.isParam()){
//取得普通表单域的值
if(name.equals("type")){
ParamPart paramPart = (ParamPart) part;
String value = paramPart.getStringValue("GBK");
out.print("普通表单域部分:<br> name=" + name + "; value=" + value + "<br>");
type = value;
}
}
//对于文件域
else if (part.isFile()){
//取得文件上传域
FilePart filePart = (FilePart) part;
String fileName = filePart.getFileName();
if (fileName != null){
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
String time = sdf.format(new java.util.Date());
//输出文件内容
long size = filePart.writeTo(new File(saveDirectory));
String tempFileName= saveDirectory+"\\" + fileName;
String lastFilePath = saveDirectory+"\\"+time+fileName.substring(fileName.lastIndexOf("."));
new File(tempFileName).renameTo(new File(lastFilePath));
if(lastFilePath.indexOf("\\\\")==-1){
String strtemp ="";
for(int i=0; i<lastFilePath.length();i++){
if(lastFilePath.charAt(i)=='\\'){
strtemp+="\\\\";
}else{
strtemp+=lastFilePath.charAt(i);
}
}
lastFilePath = strtemp;
}
out.println("上传文件:<br> 文件域的名=" + name + "; 文件名=" + lastFilePath + "<br>" +
"上传文件的路径=" + filePart.getFilePath()+"<br>"
+"文件内容类型=" + filePart.getContentType() +
"<br>"+"文件大小=" + size + "<br>");
mdbm.open();
int i = mdbm.executeUpdate("insert into test (name,type) values ('"+lastFilePath+"','"+type+"') ");
mdbm.close();
}
//文件名为空
else{
//该文件域没有输入文件名
out.println("file: name=" + name + "; EMPTY");
}
out.flush();
}
}
mdbm.open();
java.sql.ResultSet rs = mdbm.executeQuery("select * from test");
while(rs!=null && rs.next()){
%>
<img src="<%=rs.getString("name")%>" /> <%=rs.getString("type")%> <br />
<%
}
mdbm.close();
%>
cos上传文件
最新推荐文章于 2024-09-11 11:47:52 发布