java下载txt文件的简单介绍

华为云服务器特价优惠火热进行中!

2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。

合作流程:
1、点击链接注册/关联华为云账号:点击跳转
2、添加客服微信号:cloud7591,确定产品方案、价格方案、服务支持方案等;
3、客服协助购买,并拉微信技术服务群,享受一对一免费技术支持服务;
技术专家在金蝶、华为、腾讯原厂有多年工作经验,并已从事云计算服务8年,可对域名、备案、网站搭建、系统部署、AI人工智能、云资源规划等上云常见问题提供更专业靠谱的服务,对相应产品提供更优惠的报价和方案,欢迎咨询。

本篇文章给大家谈谈java下载txt文件,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

微信号:cloud7591
如需了解更多,欢迎添加客服微信咨询。
复制微信号

本文目录一览:

java 关于创建txt文件并下载的实现方法

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.RandomAccessFile;

public class Io{

public static void main(String [] s){

File filename = new File("F:\\suncity.txt");

String filein="你好!";

RandomAccessFile mm = null;

try {

mm = new RandomAccessFile(filename,"rw");

mm.writeBytes(filein);

} catch (IOException e1) {

// TODO 自动生成 catch 块

e1.printStackTrace();

} finally{

if(mm!=null){

try {

mm.close();

} catch (IOException e2) {

// TODO 自动生成 catch 块

e2.printStackTrace();

}

}

}

}

}

JAVA下载txt文件,在linux服务器tomcat用户下,有个11.txt文件,我该如何下载这个文件到本地D盘目录下?

你的代码没错,ganymed-ssh2-build210.jar 亲测

除非不存在d盘

把异常打出来看看

java代码怎样将oracle数据库中数据下载本地,为.txt文件或者.excel文件。

第一个类:

package totabel.action;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import topdf.TableToPdf;

import totabel.view.TabelData;

import totabel.xls.ExcelDemo;

public class TableAction implements ActionListener {

TabelData data;

public TableAction(TabelData data) {

this.data = data;

}

public void actionPerformed(ActionEvent e) {

String str = e.getActionCommand();

if ("添加".equals(str)) {

data.addData();

} else if ("导出到Excel".equals(str)) {

ExcelDemo demo = new ExcelDemo();

demo.method(data);

} else if ("删除".equals(str)) {

if (data.getRow() != -1) {

data.delRow();

} else {

JOptionPane.showMessageDialog(null, "请选择要删除的行!");

}

}else if("从Excel导入".equals(str)){

data.getXlsInfo();

}else if("从Excel导入到数据库".equals(str)){

data.toDb();

}else if("从table导出到pdf".equals(str)){

TableToPdf pdf=new TableToPdf();

pdf.newPage(data);

}else if("计算学分".equals(str)){

data.getXlsInfoToCredit();

}

}

}

第二个类:数据库连接

package totabel.db;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class JdbcConnection {

private static JdbcConnection con;

public static JdbcConnection getCon() {

if (con == null) {

con = new JdbcConnection();

}

return con;

}

public Connection getConnection() {

Connection connection=null;

try {

Class.forName("oracle.jdbc.OracleDriver");

String url = "jdbc:oracle:thin:@127.0.0.1:1521:oracle";

String user = "scott";

String password = "tiger";

connection = DriverManager.getConnection(url, user,

password);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

return connection;

}

// public static void main(String[] args) {

// JdbcConnection connection=new JdbcConnection();

// connection.getConnection("asd", "99");

// }

}

第三个类:主类(入口)

package totabel.db;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class JdbcConnection {

private static JdbcConnection con;

public static JdbcConnection getCon() {

if (con == null) {

con = new JdbcConnection();

}

return con;

}

public Connection getConnection() {

Connection connection=null;

try {

Class.forName("oracle.jdbc.OracleDriver");

String url = "jdbc:oracle:thin:@127.0.0.1:1521:oracle";

String user = "scott";

String password = "tiger";

connection = DriverManager.getConnection(url, user,

password);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

return connection;

}

// public static void main(String[] args) {

// JdbcConnection connection=new JdbcConnection();

// connection.getConnection("asd", "99");

// }

}

第四个类:

package totabel.xls;

import java.io.File;

import java.io.IOException;

import java.util.Vector;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import javax.swing.JOptionPane;

import totabel.view.TabelData;

import jxl.Cell;

import jxl.Sheet;

import jxl.Workbook;

import jxl.read.biff.BiffException;

import jxl.write.Label;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import jxl.write.WriteException;

import jxl.write.biff.RowsExceededException;

public class ExcelDemo {

/**

*

* @param args

*/

private Vector title = new Vector();

private Vector[] array;

// public static void main(String[] args) {

// ExcelDemo demo = new ExcelDemo();

// demo.getXlsInfo();

//

// }

public void method(TabelData table) {

int row = table.getRowSize();

int column = table.getColumnSize();

WritableWorkbook book = null;

Vector title = table.setTitle();

Object[] str = title.toArray();

try {

book = Workbook.createWorkbook(new File("test.xls"));

WritableSheet sheet = book.createSheet("成绩表", 0);

for (int i = 0; i str.length; i++) {

sheet.addCell(new Label(i, 0, (String) str[i]));

}

for (int i = 1; i row + 1; i++) {

for (int j = 1; j column + 1; j++) {

sheet.addCell(new Label(j - 1, i, table.getTableInfo(i - 1,

j - 1)));

}

}

book.write();

JOptionPane.showMessageDialog(null, "导出完成!");

} catch (IOException e) {

e.printStackTrace();

} catch (RowsExceededException e) {

e.printStackTrace();

} catch (WriteException e) {

e.printStackTrace();

} finally {

try {

book.close();

} catch (WriteException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 输出Excel的数据到表单

*

* @return

*/

public Vector getXlsInfo() {

Vector v = new Vector();

jxl.Workbook rwb = null;

int index = 0;

try {

rwb = jxl.Workbook.getWorkbook(new File("test.xls"));

Sheet[] sheet = rwb.getSheets();

for (int i = 0; i sheet.length; i++) {

int rs = sheet[i].getRows();

array = new Vector[rs - 1];

for (int j = 1; j rs; j++) {

Cell[] cell = sheet[i].getRow(j);

Vector info = new Vector();

for (int k = 0; k cell.length; k++) {

info.add(cell[k].getContents());

}

array[index] = info;

index++;

v.add(info);

}

Cell[] titleCell = sheet[i].getRow(0);

for (int j = 0; j titleCell.length; j++) {

title.add(titleCell[j].getContents());

}

}

} catch (BiffException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

rwb.close();

}

return v;

}

public Vector getXlsInfoToCredit() {

Vector v = new Vector();

jxl.Workbook rwb = null;

try {

rwb = jxl.Workbook.getWorkbook(new File("d:/test/信科0821(南迁).xls"));

Sheet[] sheet = rwb.getSheets();

for (int i = 0; i sheet.length; i++) {

int rs = sheet[i].getRows();

array = new Vector[rs - 1];

for (int j = 1; j rs; j++) {

Cell[] cell = sheet[i].getRow(j);

Vector info = new Vector();

for (int k = 0; k cell.length; k++) {

// if(){

Pattern p = Pattern.compile("[0-9]{1,}");

Matcher m = p.matcher(cell[k].getContents());

if (m.matches()) {

int score = Integer.valueOf(cell[k].getContents());

float result = getScore(score);

info.add(result);

} else {

info.add(cell[k].getContents());

}

}

v.add(info);

}

Cell[] titleCell = sheet[i].getRow(0);

for (int j = 0; j titleCell.length; j++) {

title.add(titleCell[j].getContents());

}

}

} catch (BiffException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

rwb.close();

}

return v;

}

public float getScore(int n) {

float score = n;

if (n 60) {

score = 0;

return score;

} else {

if (n = 60 n = 63) {

score = (float) 1.0;

} else if (n = 64 n = 67) {

score = (float) 1.3;

} else if (n = 68 n = 71) {

score = (float) 1.7;

} else if (n = 72 n = 75) {

score = (float) 2.0;

} else if (n = 76 n = 79) {

score = (float) 2.3;

} else if (n = 80 n = 83) {

score = (float) 2.7;

} else if (n = 84 n = 87) {

score = (float) 3.0;

} else if (n = 88 n = 91) {

score = (float) 3.3;

} else if (n = 92 n = 95) {

score = (float) 3.7;

} else if (n = 96 n = 100) {

score = (float) 4.0;

}

return score;

}

}

public Vector getTitle() {

// getXlsInfo();

return title;

}

public Vector[] getArray() {

getXlsInfo();

return array;

}

}

因为时间问题就没有再写了,上面是我以前做的,不懂就q我

java 关于浏览器下载文件

浏览器默认可以打开图片和文本。word和excel属于文件,浏览器打不开的,你要设置浏览器属性才能默认打开(以前设置过,忘记怎么设置了,自己百度)

怎样通过java实现服务器上文件下载?

在jsp/servlet中断点/多线程下载文件

%@ page import="java.io.File" %

%@ page import="java.io.IOException" %

%@ page import="java.io.OutputStream" %

%@ page import="java.io.RandomAccessFile" %

%!

    public void downloadFile(HttpServletRequest request, HttpServletResponse response, File file) throws IOException {

        RandomAccessFile raf = new RandomAccessFile(file, "r");

        java.io.FileInputStream fis = new java.io.FileInputStream(raf.getFD());

        response.setHeader("Server", "");

        response.setHeader("Accept-Ranges", "bytes");

        long pos = 0;

        long len;

        len = raf.length();

        if (request.getHeader("Range") != null) {

            response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);

            pos = Long.parseLong(request.getHeader("Range")

                    .replaceAll("bytes=", "")

                    .replaceAll("-", "")

            );

        }

        response.setHeader("Content-Length", Long.toString(len - pos));

        if (pos != 0) {

            response.setHeader("Content-Range", new StringBuffer()

                    .append("bytes ")

                    .append(pos)

                    .append("-")

                    .append(Long.toString(len - 1))

                    .append("/")

                    .append(len)

                    .toString()

            );

        }

        response.setContentType("application/octet-stream");

        response.setHeader("Content-Disposition", new StringBuffer()

                .append("attachment;filename=\"")

                .append(file.getName())

                .append("\"").toString());

        raf.seek(pos);

        byte[] b = new byte[2048];

        int i;

        OutputStream outs = response.getOutputStream();

        while ((i = raf.read(b)) != -1) {

            outs.write(b, 0, i);

        }

        raf.close();

        fis.close();

    }

%

%

    String filePath = request.getParameter("file");

    filePath = application.getRealPath(filePath);

    File file = new File(filePath);

    downloadFile(request, response, file);

%

java下载txt文件的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、java下载txt文件的信息别忘了在本站进行查找喔。

发布于 2023-04-07 11:04:26
收藏
分享
海报
33
目录

    忘记密码?

    图形验证码

    复制成功
    微信号: cloud7591
    如需了解更多,欢迎添加客服微信咨询。
    我知道了