真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java文件下載保存代碼,JAVA下載文件

Java中文件下載該怎么寫代碼求高手指導(dǎo)

你的上傳做了嗎?

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了玉溪免費建站歡迎大家使用!

if (upfile.exists()) {

bytes = FileUtils.readFileToByteArray(upfile);

response.setContentType("application/x-download");

String agent = request.getHeader("USER-AGENT");//用戶代理

// 防止中文文件名亂碼

if (null != agent -1 != agent.indexOf("MSIE")) {

String codedfilename = StringUtils.replace(URLEncoder.encode(fileName, "UTF-8"), "+", "%20");

response.setHeader("Content-Disposition", "attachment;filename=" + codedfilename);

} else if (null != agent -1 != agent.indexOf("Mozilla")) {

String codedfilename = MimeUtility.encodeText(fileName, "UTF-8", "B");

response.setHeader("Content-Disposition", "attachment;filename=" + codedfilename);

} else {

response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

}

response.setContentLength(bytes.length);

response.getOutputStream().write(bytes);

}

java下載服務(wù)器上的文件到客戶端

java編程方法下載服務(wù)器上的文件到本地客服端,代碼如下:

import?java.io.BufferedWriter;

import?java.io.File;

import?java.io.FileOutputStream;

import?java.io.FileWriter;

import?java.io.IOException;

import?java.io.InputStream;

import?java.net.URL;

import?java.net.URLConnection;

public?class?DownLoad?{???

public?static?void?downloadFile(URL?theURL,?String?filePath)?throws?IOException?{??

File?dirFile?=?new?File(filePath);

if(!dirFile.exists()){?

//文件路徑不存在時,自動創(chuàng)建目錄

dirFile.mkdir();

}

//從服務(wù)器上獲取圖片并保存

URLConnection?connection?=?theURL.openConnection();

InputStream?in?=?connection.getInputStream();??

FileOutputStream?os?=?new?FileOutputStream(filePath+"\\123.png");?

byte[]?buffer?=?new?byte[4?*?1024];??

int?read;??

while?((read?=?in.read(buffer))??0)?{??

os.write(buffer,?0,?read);??

}??

os.close();??

in.close();

}???

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

//下面添加服務(wù)器的IP地址和端口,以及要下載的文件路徑

String?urlPath?=?"http://服務(wù)器IP地址:端口/image/123.png";?

//下面代碼是下載到本地的位置

String?filePath?=?"d:\\excel";?

URL?url?=?new?URL(urlPath);?

try?{?

downloadFile(url,filePath);?

}?catch?(IOException?e)?{?

e.printStackTrace();?

}?

}???

}

Java 下載文件的方法怎么寫

參考下面

public HttpServletResponse download(String path, HttpServletResponse response) {

try {

// path是指欲下載的文件的路徑。

File file = new File(path);

// 取得文件名。

String filename = file.getName();

// 取得文件的后綴名。

String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

// 以流的形式下載文件。

InputStream fis = new BufferedInputStream(new FileInputStream(path));

byte[] buffer = new byte[fis.available()];

fis.read(buffer);

fis.close();

// 清空response

response.reset();

// 設(shè)置response的Header

response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));

response.addHeader("Content-Length", "" + file.length());

OutputStream toClient = new BufferedOutputStream(response.getOutputStream());

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

toClient.write(buffer);

toClient.flush();

toClient.close();

} catch (IOException ex) {

ex.printStackTrace();

}

return response;

}

// 下載本地文件

public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {

String fileName = "Operator.doc".toString(); // 文件的默認(rèn)保存名

// 讀到流中

InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路徑

// 設(shè)置輸出的格式

response.reset();

response.setContentType("bin");

response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

// 循環(huán)取出流中的數(shù)據(jù)

byte[] b = new byte[100];

int len;

try {

while ((len = inStream.read(b)) 0)

response.getOutputStream().write(b, 0, len);

inStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

// 下載網(wǎng)絡(luò)文件

public void downloadNet(HttpServletResponse response) throws MalformedURLException {

int bytesum = 0;

int byteread = 0;

URL url = new URL("windine.blogdriver.com/logo.gif");

try {

URLConnection conn = url.openConnection();

InputStream inStream = conn.getInputStream();

FileOutputStream fs = new FileOutputStream("c:/abc.gif");

byte[] buffer = new byte[1204];

int length;

while ((byteread = inStream.read(buffer)) != -1) {

bytesum += byteread;

System.out.println(bytesum);

fs.write(buffer, 0, byteread);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

//支持在線打開文件的一種方式

public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {

File f = new File(filePath);

if (!f.exists()) {

response.sendError(404, "File not found!");

return;

}

BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));

byte[] buf = new byte[1024];

int len = 0;

response.reset(); // 非常重要

if (isOnLine) { // 在線打開方式

URL u = new URL("" + filePath);

response.setContentType(u.openConnection().getContentType());

response.setHeader("Content-Disposition", "inline; filename=" + f.getName());

// 文件名應(yīng)該編碼成UTF-8

} else { // 純下載方式

response.setContentType("application/x-msdownload");

response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());

}

OutputStream out = response.getOutputStream();

while ((len = br.read(buf)) 0)

out.write(buf, 0, len);

br.close();

out.close();

}

java如何保存文件

這是我原來做的例子,里面有文件儲存的內(nèi)容,代碼不多,給你參考參考.

/**

* 五個按鈕的故事,西西哈。

*/

import java.awt.*;

import java.awt.event.*;

import java.io.*;

public class FileMessage extends Frame implements ActionListener

{

private static final long serialVersionUID = 10L;

Dialog dia;

private Panel p;

private File fi;

Process po=null;

private String s;

private TextArea ta;

private FileDialog fd;

private Button b1,b2,b3,b4,b5;

private Button b6;

public FileMessage()

{

super("文本文件處理");

setBackground( Color.LIGHT_GRAY );

setLocation(200,300);

setResizable( false);

setVisible( true);

addWindowListener( new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit( 0);

}

});

}

public void init()

{

ta=new TextArea("\n\n\n\n\n\t\t\t\t文本顯示區(qū)");

ta.setSize(30,5);

ta.setEditable(false);

add( ta,"North");

p=new Panel();

add( p,"Center");

b1=new Button("瀏覽");

b2=new Button("保存");

b3=new Button("清空");

b4=new Button("關(guān)閉");

b5=new Button("獨立打開");

b6=new Button("確定");

p.add(b1);

p.add(b2);

p.add(b3);

p.add(b4);

p.add(b5);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

b6.addActionListener(this);

fd=new FileDialog(this,"請選擇文件",FileDialog.LOAD);

fd.setDirectory("f:\\note");

pack();

dia=new Dialog(this,"注意",true);

dia.setLayout(new BorderLayout());

Panel p1=new Panel();

p1.add( b6);

dia.add(new Label(" 請先選擇文件"),BorderLayout.CENTER);

dia.add( p1,BorderLayout.SOUTH);

dia.addWindowListener( new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

dia.setVisible( false);

}

});

dia.setLocation(310,370);

dia.setSize(200,130);

}

public static void main(String[] args)

{

new FileMessage().init();

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==b1)

{

fd.setVisible(true);

s=fd.getDirectory()+fd.getFile();

fi=new File(s);

byte[] b=new byte[(int)fi.length()];

try

{

new FileInputStream(fi).read(b);

ta.setText(new String(b,0,(int)fi.length()));

}

catch(Exception e1){}

ta.setEditable(true);

}

else if(e.getSource()==b2)

{

try

{

if(ta.getText().equals("保存成功")||ta.getText() .equals( ""))

{}

else

{

new FileOutputStream(fi).write(ta.getText().getBytes());

ta.setText("保存成功");

ta.setEditable(false);

}

}

catch(FileNotFoundException e1)

{

ta.setText(e1.getMessage());

}

catch(IOException e1)

{

ta.setText("出現(xiàn)IOException異常");

}

}

else if(e.getSource()==b4)

System.exit(0);

else if(e.getSource()==b3)

{

ta.setText("");

ta.setEditable( false);

}

else if(e.getSource()==b5)

{

if(s==null)

{

dia.setVisible(true);

}

else

{

try

{

po=Runtime.getRuntime().exec("notepad.exe "+s);

}

catch(Exception ei)

{}

}

}

else if(e.getSource() ==b6)

{

dia.setVisible(false);

}

}

}


新聞名稱:java文件下載保存代碼,JAVA下載文件
文章出自:http://weahome.cn/article/dssdeds.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部