科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道[冷枫]Java对各种文件的操作详解

[冷枫]Java对各种文件的操作详解

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

Java对各种文件的操作详解

作者:冷枫 来源:CSDN 2007年9月23日

关键字:

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共2页)

数据库字段中的文件下载
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*,java.sql.*,java.io.*"%>
<%
    String id = request.getParameter("id");
    if(id==null)
    {   throw new Exception ("没有找到图片");
    }
    else
    {
       try
       {
com.gzrealmap.lib.jdbc.JDBCUtil  SqlBean= com.gzrealmap.lib.jdbc.JDBCUtil.getInstance();
               SqlBean.connect();
               String sql = "select * from innernews where id = '"+79+"'";
               ResultSet rs = SqlBean.queryforUpdate(sql);
               rs.next();
               //String fileNamedb = rs.getString("imageName");
               String file= rs.getString("acc");
               //String fileName = new String(fileNamedb.getBytes(),"iso-8859-1");
               String fileName = "a.jpg";
                response.setHeader("Content-Disposition",  "inline; filename=\"" + fileName + "\"");    
               String filter = fileName.substring(fileName.lastIndexOf("."));
              
               if(filter.equals(".txt"))
               {
                   response.setContentType("text/plain");
               }
               else if(filter.equals(".doc")||filter.equals(".dot"))
               {
                   response.setContentType("application/msword");
               }
               else
               {
                 response.setContentType("image/jpeg;charset=GB2312");
               }
               ServletOutputStream o = response.getOutputStream();
               //o.write(file);
               out.println(file);
               //o.flush();
               //o.close();
               SqlBean.disconnect();
       }
        catch(Exception ex)
       {
           out.println(ex.getMessage());
       }
    }  
%>
 
把网页保存成文件<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.text.*,java.util.*,java.net.*,java.io.*"%>
<%
 URL stdURL = null;
 BufferedReader stdIn = null;
 PrintWriter stdOut = null;
 try {
  stdURL = new URL("http://www.163.com");
 }
 catch (MalformedURLException e) {
   throw e;
 }
 
try {
    //将字节流转变成为字符流
    stdIn = new BufferedReader(new InputStreamReader(stdURL.openStream()));
    String theFileName = "c://测试//163.html";
    stdOut = new PrintWriter(new BufferedWriter(new FileWriter(theFileName.toString())));
 }
 catch (IOException e) {
 }
 
 /***把URL指定的页面以流的形式读出,写成指定的文件***/
 try {
    String strHtml = "";
   while((strHtml = stdIn.readLine())!=null) {
   stdOut.println(strHtml);
   }
 }
 catch (IOException e) {
   throw e;
 }
 finally {  
   try {
     if(stdIn != null)
       stdIn.close();
     if(stdOut != null)
       stdOut.close();
       }
   catch (Exception e) {
     System.out.println(e);
   }
 }
%>
 
直接下载网上的文件
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%
  int bytesum=0;
  int byteread=0;
  URL url = new URL("http://pimg.163.com/sms/micheal/logo.gif");
  URLConnection conn = url.openConnection();
  InputStream inStream = conn.getInputStream();
 
  /**
  String theFileName = "c:/测试/logo.gif";
  theFileName = theFileName.toString();
  File myFilePath=new File(theFileName);
  if(!myFilePath.exists())
  myFilePath.createNewFile();
  **/
 
  FileOutputStream fs=new FileOutputStream("c:/测试/logo2.gif");
  byte[]  buffer =new  byte[1444];
    while ((byteread=inStream.read(buffer))!=-1)
    {
       out.println("<DT><B>"+byteread+"</B></DT>");
       bytesum+=byteread;
       //System.out.println(bytesum);
       fs.write(buffer,0,byteread);
     }
%>
 
按行读文件 <%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*" %>
<%
FileReader myFileReader=new FileReader("c:/哈哈.txt");
BufferedReader myBufferedReader=new BufferedReader(myFileReader);
String myString=null;
String resultString=new String();
while((myString=myBufferedReader.readLine())!=null) {
resultString=resultString+myString+"<br>";
}
out.println(resultString);
myFileReader.close();
%>
 
对word文档的处理(上传与下载)<%@ page contentType="application/msword" %>
<!-- 以上这行设定本网页为excel格式的网页 -->
<%
   response.setHeader("Content-disposition","inline; filename=test1.doc"); //线上浏览方式
  // response.setHeader("Content-disposition","attachment; filename=test1.doc");//下载方式
   //以上这行设定传送到前端浏览器时的档名为test1.doc
   //就是靠这一行,让前端浏览器以为接收到一个word档
%>
//然后输出动态内容就可以得到一个word文档了
 
1,打开:
1)文件头上加:<%@ page  contentType="application/msword"%> 
xml文件里:
<mime-mapping>
        <extension>doc</extension>
        <mime-type>application/msword</mime-type>
</mime-mapping>
2)可以用js,以下代码来自引用:
<%@ page contentType="text/html;charset=gb2312" import= "java.io.*"%>
<HTML>
<script>
var wrd=new ActiveXObject("Word.Application")
wrd.visible=true
alert("您的"+wrd.Application.Caption+"安装路径为:\n"+wrd.Application.Path+"\n版本号是:"+wrd.Application.version+"\n注册使用者是:"+wrd.Application.UserName)
wrd.Documents.Add()
//wrd.Documents.Open("c:\\exam.doc")
wrd.Selection.TypeText("This is some text.")
wrd.Application.Activate()
wrd.ActiveDocument.SaveAs("c:\\exam111.doc")
wrd=null
</script>
</HTML>
 
2,下载:
<%@ page contentType="text/html;charset=gb2312" import= "java.io.*"%>
<%// 得到文件名字和路径
  String filename = "jsp.doc";
  String filepath = "C:\\";
 
  // 设置响应头和下载保存的文件名
  response.setContentType("APPLICATION/OCTET-STREAM");
  response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
 
  // 打开指定文件的流信息
  java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath + filename);
  //FileOutputStream out  = new FileOutputStream(filepath+"测试\\" + filename);
  // 写出流信息
  int i;
  while ((i=fileInputStream.read()) != -1) {
   out.write(i);
  }
  fileInputStream.close();
  out.close();
 %>

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=726880

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章