public static void readDBF(String path)
{
InputStream fis = null
try
{
//读取文件的输入流
fis = new FileInputStream(path)
//根据输入流初始化一个DBFReader实例,用来读取DBF文件信息
DBFReader reader = new DBFReader(fis)
//调用DBFReader对实例方法得到path文件中字段的个数
int fieldsCount = reader.getFieldCount()
System.out.println("字段数:"+fieldsCount)
//取出字段信息
for( int i=0 i<fieldsCount i++)
{
DBFField field = reader.getField(i)
System.out.println(field.getName())
}
Object[] rowValues
//一条条取出path文件中记录
while((rowValues = reader.nextRecord()) != null)
{
for( int i=0 i<rowValues.length i++)
{
System.out.println(rowValues[i])
}
}
}
catch(Exception e)
{
e.printStackTrace()
}
finally
{
try{
fis.close()
}catch(Exception e){}
}
}
public static void writeDBF(String path)
{
OutputStream fos = null
try
{
//定义DBF文件字段
DBFField[] fields = new DBFField[3]
//分别定义各个字段信息,setFieldName和setName作用相同,
//只是setFieldName已经不建议使用
fields[0] = new DBFField()
//fields[0].setFieldName("emp_code")
fields[0].setName("semp_code")
fields[0].setDataType(DBFField.FIELD_TYPE_C)
fields[0].setFieldLength(10)
fields[1] = new DBFField()
//fields[1].setFieldName("emp_name")
fields[1].setName("emp_name")
fields[1].setDataType(DBFField.FIELD_TYPE_C)
fields[1].setFieldLength(20)
fields[2] = new DBFField()
//fields[2].setFieldName("salary")
fields[2].setName("salary")
fields[2].setDataType(DBFField.FIELD_TYPE_N)
fields[2].setFieldLength(12)
fields[2].setDecimalCount(2)
//DBFWriter writer = new DBFWriter(new File(path))
//定义DBFWriter实例用来写DBF文件
DBFWriter writer = new DBFWriter()
//把字段信息写入DBFWriter实例,即定义表结构
writer.setFields(fields)
//一条条的写入记录
Object[] rowData = new Object[3]
rowData[0] = "1000"
rowData[1] = "John"
rowData[2] = new Double(5000.00)
writer.addRecord(rowData)
rowData = new Object[3]
rowData[0] = "1001"
rowData[1] = "Lalit"
rowData[2] = new Double(3400.00)
writer.addRecord(rowData)
rowData = new Object[3]
rowData[0] = "1002"
rowData[1] = "Rohit"
rowData[2] = new Double(7350.00)
writer.addRecord(rowData)
//定义输出流,并关联的一个文件
fos = new FileOutputStream(path)
//写入数据
writer.write(fos)
//writer.write()
}catch(Exception e)
{
e.printStackTrace()
}
finally
{
try{
fos.close()
}catch(Exception e){}
}
}
public static void main(String[] args){
String path ="E:\\tmp\\2\\xx.dbf"
try {
InputStream fis = new FileInputStream(path)
DBFReader reader = new DBFReader(fis)
int fieldsCount = reader.getFieldCount()
System.out.println("字段数:"+fieldsCount)
DBFField[] df = new DBFField[fieldsCount+2]
for( int i=0 i<fieldsCount i++)
{
df[i] = reader.getField(i)
System.out.println("field"+i+":"+df[i].getName())
}
df[fieldsCount] = new DBFField()
df[fieldsCount].setName("add1")
df[fieldsCount].setDataType(DBFField.FIELD_TYPE_C)
df[fieldsCount].setFieldLength(10)
df[fieldsCount+1] = new DBFField()
df[fieldsCount+1].setName("add2")
df[fieldsCount+1].setDataType(DBFField.FIELD_TYPE_C)
df[fieldsCount+1].setFieldLength(10)
DBFWriter writer = new DBFWriter()
writer.setFields(df)
Object[] rowValues
Object[] rowValues1 = new Object[fieldsCount+2]
//一条条取出path文件中记录
while((rowValues = reader.nextRecord()) != null)
{
for(int i=0i<fieldsCounti++){
rowValues1[i] = rowValues[i]
}
rowValues1[fieldsCount]="x"
rowValues1[fieldsCount+1]="xx"
writer.addRecord(rowValues1)
}
path ="E:\\tmp\\2\\test2.dbf"
OutputStream fos = new FileOutputStream(path)
//写入数据
writer.write(fos)
System.out.println("OVER")
} catch (FileNotFoundException | DBFException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
详细出处参考:http://www.jb51.net/article/46344.htm
ml页面的一个链接中。这样做的缺点是把文件在服务器上的路径暴露了,并且还无法对文件下载进行其它的控制(如权限)。这个就不写示例了。2.在服务器端把文件转换成输出流,写入到response,以response把文件带到浏览器,由浏览器来提示用户是否愿意保存文件到本地。(示例如下)
<%
response.setContentType(fileminitype)
response.setHeader("Location",filename)
response.setHeader("Cache-Control", "max-age=" + cacheTime)
//filename应该是编码后的(utf-8)
response.setHeader("Content-Disposition", "attachmentfilename=" + filename)
response.setContentLength(filelength)
OutputStream outputStream = response.getOutputStream()
InputStream inputStream = new FileInputStream(filepath)
byte[] buffer = new byte[1024]
int i = -1
while ((i = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, i)
}
outputStream.flush()
outputStream.close()
inputStream.close()
outputStream = null
%>
3.既然是JSP的话,还有一种方式就是用Applet来实现文件的下载。不过客户首先得信任你的这个Applet小程序,由这个程序来接受由servlet发送来的数据流,并写入到本地。
servlet端示例
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType(" text/plain ")
OutputStream outputStream = null
try {
outputStream = res.getOutputStream()
//把文件路径为srcFile的文件写入outputStream中
popFile(srcFile, outputStream))
} catch (IOException e) {
e.printStackTrace()
}
}
JApplet端示例
URLConnection con
try {
//url是被调用的SERVLET的网址 如 *.do
con = url.openConnection()
con.setUseCaches(false)
con.setDoInput(true)
con.setDoOutput(true)
con.setRequestProperty("Content-Type",
"application/octet-stream")
InputStream in = con.getInputStream()
ProgressMonitorInputStream pmInputStream = new ProgressMonitorInputStream
(pane, "正在从服务器下载文件内容", in)
ProgressMonitor pMonitor = pmInputStream.getProgressMonitor()
pMonitor.setMillisToDecideToPopup(3)
pMonitor.setMillisToPopup(3)
//localfilepath本地路径,localstr文件文件夹,filename本地文件名
String localfilepath = localstr + filename
//方法saveFilsaveFilee是把输入流pmInputStream写到文件localfilepath中
if(saveFilsaveFilee(localfilepath,pmInputStream)){
openLocalFile(localfilepath)
}
4.顺便把JApplet上传文件的代码也贴上来.
JApplet端示例
URLConnection con
try {
con = url.openConnection()
//url是被调用的SERVLET的网址 如 *.do
con.setUseCaches(false)
con.setDoInput(true)
con.setDoOutput(true)
con.setRequestProperty("Content-Type","application/octet-stream")
OutputStream out = con.getOutputStream()
//localfilepath本地路径,localstr文件文件夹,filename本地文件名
String localfilepath = localstr + filename
//文件getOutputStream是把文件localfilepath写到输出流out中
getOutputStream(localfilepath,out)
InputStream in = con.getInputStream()
return true
}catch (IOException e) {
System.out.println("文件上传出错!")
e.printStackTrace()
}
servlet端代码示例
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType(" text/plain ")
InputStream inputStream = null
try {
inputStream = res.getInputStream()
//把输入流inputStream保存到文件路径为srcFile的文件中
writefile(srcFile, inputStream)
} catch (IOException e) {
e.printStackTrace()
}
} // end service
总结:在文件的传输中是流的形式存在的,在硬盘上是文件的形式存在的。我们要做的只是通过HttpServletRequest和HttpServletResponse,或者是response和request来发送流和读取流。以及把文件转换成流或把流转换成文件的 *** 作。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)