我已经能把服务器本机的文件存储到数据库的BLOB字段中,但不知道如何把异地的文件也存储到数据库服务器中,请各位高手指教。
解决方案 »
- 求助!用JAVA 做个作业。。需求各种强人哥帮忙!!!The application is a currency application
- throw与throws的区别,什么时候用?
- 用过ThreadPoolExecutor的人,该怎么设计?
- 多线程问题,进来看看
- 菜鸟送分题javascript
- Exception in thread "main" java.lang.NoClassDefFoundError: a 在线等
- 在jbuilder 里面能够获取数据库数据,导成.exe文件后就不能获取了,怎么回事啊??
- 关于图片的处理问题!
- 求swing方面的文档资料!
- 那位大虾能告诉我Class类的使用方法,或者相关文档
- java中如何不用设dsn访问数据库
- 关于得到服务器目录结构--求助
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<html>
<head>
<title>
万科
</title>
</head>
<body bgcolor="#ffffff">
<center>
<%
int n=0;
int MAX_SIZE = 102400 * 102400;//定义上载文件的最大字节
String rootPath; // 创建根路径的保存变量
DataInputStream in = null; //声明文件读入类
FileOutputStream fileOut = null;
String remoteAddr = request.getRemoteAddr();//取得客户端的网络地址IP
//out.print(remoteAddr);
//out.print("<br>");
String serverName = request.getServerName(); //获得服务器的名字
//你访问的网站是:<%out.print(serverName);
//out.print("<br>");
//取得jsp文件相对与根地址的地址
//out.print(request.getServletPath());
//out.print("<br>");
String realPath = request.getRealPath(serverName); //取得互联网程序的绝对地址
//out.println(realPath);
//out.print("<br>");
realPath = realPath.substring(0,realPath.lastIndexOf("\\"));
//out.print(realPath);
rootPath = realPath + "\\upload\\"; //创建文件的保存目录upload
//传到服务器上的路径是:<%out.println(rootPath); String contentType = request.getContentType(); //取得客户端上传的数据类型
//out.println("<p>客户端上传的数据类型 = " + contentType + "</p>");
try{
/*if(contentType != null && contentType.indexOf("multipart/form-data") >= 0)
{ */
//读入上传的数据
in = new DataInputStream(request.getInputStream());
int formDataLength=request.getContentLength();
if(formDataLength > MAX_SIZE){
out.print("<P>上传的文件字节数不可以超过"+ MAX_SIZE + "</p>");
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(saveFile.indexOf(".txt")<0)
{
out.print("<p>你上传的文件不是文本文件!</p>");
}
else {
if(checkFile.exists())
{
out.println("<p>" + saveFile + "文件已经存在.</p>");
}
//检查上载文件的目录是否存在
File fileDir = new File(rootPath);
if(!fileDir.exists())
{
fileDir.mkdirs();
}
//创建文件的写出类
fileOut = new FileOutputStream(fileName);
//保存文件的数据
fileOut.write(dataBytes,startPos,(endPos - startPos));
fileOut.close();
out.println("<P>" + saveFile + "文件成功上载.</p>");
/*}else{
String content = request.getContentType();
out.println
("<p>上传的数据类型不是是multipart/form-data</p>");
} */
%><%
FileReader fr=new FileReader(rootPath +saveFile);
//建立BufferedReader变量, 并设定由br变量引用
BufferedReader br=new BufferedReader(fr);
//自文件读取一行字串
String Line=br.readLine();
String strsql="";
//判断读取到的字串是否不等于null
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://100.60.69.251:1433;DatabaseName=车行";//car你的数据库的
String user="car";//数据库用户名
String pass="car";//数据库密码
Connection conn=DriverManager.getConnection(url,user,pass); Statement smt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE , ResultSet.CONCUR_READ_ONLY);
while(Line!=null)
{
strsql="insert into 短信发送(手机号码) values('"+Line+"')";
smt.executeUpdate(strsql);
n++;
Line = br.readLine(); //自文件读取一行数据
}
br.close(); //关闭BufferedReader变量
fr.close(); //关闭文件smt.close();
conn.close();%><h4>共导入数据库<%= n %>条记录</h4><%
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
}catch(Exception ex)
{
throw new ServletException(ex.getMessage());
} %>
</center> </body>
</html>