JAVA里怎么生成txt并保存到局域网内某台计算机的指定文件夹里

JAVA里怎么生成txt并保存到局域网内某台计算机的指定文件夹里,第1张

我有个 文件传输的程序~~~ 可能你需要改一下你的 IP!!

通过Socket 处理IO流!(远程都测试过的 绝对行!)

流程是这样的~~:

客户端选择文件,并发送!

服务端接受数据,并保存为文件!

//////////////////////////////////////////////客户端程序:

////////////////////文件一:Windowjava

package clientwindow;

import clientChooseFile;

import clientSendFile;

import javaxswingJOptionPane;

import javaawteventMouseAdapter;

import javaawteventMouseEvent;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJLabel;

import javaxswingJMenu;

import javaxswingJMenuBar;

import javaxswingJMenuItem;

import javaxswingJTextArea;

import javaxswingJTextField;

public class Window {

public boolean ready;

public static JTextArea textArea;

private JTextField portTextField;

private JTextField hostTextField;

private JTextField fileTextField;

private JFrame frame;

/

Launch the application

@param args

/

public static void main(String args[]) {

try {

Window window = new Window();

windowframesetVisible(true);

} catch (Exception e) {

eprintStackTrace();

}

}

/

Create the application

/

public Window() {

initialize();

}

/

Initialize the contents of the frame

/

private void initialize() {

frame = new JFrame();

framesetTitle("客户端");

framegetContentPane()setLayout(null);

framesetBounds(100, 100, 390, 275);

framesetLocation(300, 250);

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

final JMenuBar menuBar = new JMenuBar();

framesetJMenuBar(menuBar);

final JMenu fileMenu = new JMenu();

fileMenusetText("文件");

menuBaradd(fileMenu);

final JMenuItem exitMenuItem = new JMenuItem();

exitMenuItemaddMouseListener(new MouseAdapter() {

public void mousePressed(MouseEvent e) {

JOptionPaneshowMessageDialog(null, "Bye Bye!");

Systemexit(1);

}

});

exitMenuItemsetText("退出");

fileMenuadd(exitMenuItem);

final JMenu helpMenu = new JMenu();

helpMenusetText("帮助");

menuBaradd(helpMenu);

final JMenuItem viewMenuItem = new JMenuItem();

viewMenuItemsetText("查看");

helpMenuadd(viewMenuItem);

final JLabel fileLabel = new JLabel();

fileLabelsetText("文件位置:");

fileLabelsetBounds(30, 40, 80, 15);

framegetContentPane()add(fileLabel);

fileTextField = new JTextField();

fileTextFieldsetBounds(116, 37, 163, 22);

framegetContentPane()add(fileTextField);

//选择文件

final JButton viewButton = new JButton();

viewButtonaddMouseListener(new MouseAdapter() {

public void mousePressed(MouseEvent e) {

new ChooseFile();

if(ChooseFileFILENAME == null || ChooseFilePATH == null){

JOptionPaneshowMessageDialog(null, "请选择存储文件!");

}

else{

fileTextFieldsetText(ChooseFilePATH);

textAreaappend("选择文件:"+ChooseFileFILENAME+"\n");

ready = true;

}

}

});

viewButtonsetText("浏览");

viewButtonsetBounds(296, 37, 78, 22);

framegetContentPane()add(viewButton);

final JLabel addLabel = new JLabel();

addLabelsetText("主机地址:");

addLabelsetBounds(30, 93, 80, 15);

framegetContentPane()add(addLabel);

hostTextField = new JTextField();

hostTextFieldsetBounds(116, 90, 163, 22);

hostTextFieldsetText("127001");

hostTextFieldsetEditable(false);

framegetContentPane()add(hostTextField);

final JLabel portLabel = new JLabel();

portLabelsetText("端口");

portLabelsetBounds(285, 93, 42, 15);

framegetContentPane()add(portLabel);

portTextField = new JTextField();

portTextFieldsetBounds(322, 90, 52, 22);

portTextFieldsetText("6666");

portTextFieldsetEditable(false);

framegetContentPane()add(portTextField);

//发送文件

final JButton sendButton = new JButton();

sendButtonaddMouseListener(new MouseAdapter() {

public void mousePressed(MouseEvent e) {

try{

if(ready){

new SendFile(fileTextFieldgetText(),"127001",6666);//这里更改IP

}

else{

JOptionPaneshowMessageDialog(null, "请选择文件!");

}

}

catch(Exception exception){

Systemoutprintln(exception);

}

}

});

sendButtonsetText("发送");

sendButtonsetBounds(296, 137, 78, 22);

framegetContentPane()add(sendButton);

//退出程序

final JButton exitButton = new JButton();

exitButtonaddMouseListener(new MouseAdapter() {

public void mousePressed(MouseEvent e) {

JOptionPaneshowMessageDialog(null, "Bye Bye!");

Systemexit(1);

}

});

exitButtonsetText("退出");

exitButtonsetBounds(296, 182, 78, 22);

framegetContentPane()add(exitButton);

final JLabel sendLabel = new JLabel();

sendLabelsetText("发送状态:");

sendLabelsetBounds(30, 140, 80, 15);

framegetContentPane()add(sendLabel);

textArea = new JTextArea();

//textAreaadd(JTextArea);

textAreasetBounds(116, 138, 163, 66);

textAreasetEditable(false);

textAreasetLineWrap(true);

framegetContentPane()add(textArea);

//textAreaadd(scrollPane);

}

}

///////////////////////////////////////////文件二:ChooseFilejava

/

/

package client;

import javaxswing;

/

@author 泥鳅

/

// 该类用于选择本地文件,并返回文件路径和文件名

public class ChooseFile {

private JFileChooser jfile;

private JDialog jd;

public static String PATH;

public static String FILENAME;

public ChooseFile(){

jfile = new JFileChooser();

jd = new JDialog();

// jdadd(jfile);

// jfilesetVisible(true);

// jdsetLocation(320, 270);

// jdsetSize(280, 230);

// jdsetVisible(true);

if(JFileChooserAPPROVE_OPTION == jfileshowOpenDialog(jd)){

// Systemoutprintln("OK");

String oldstr = jfilegetSelectedFile()getPath();

//返回路径

PATH = changePath(oldstr);

//返回文件名

FILENAME = jfilegetSelectedFile()getName();

}

}

//将路径格式转换成 java所识别的路径格式

public String changePath(String oldstr){

// String newstr;

/

for(int i=0;i<=oldstrlength();i++){

if(oldstrcharAt(i)=='\\'){

}

}

/

return oldstrreplaceAll("\\\\", "\\\\\\\\");

}

}

//////////////////////////////////////////文件三:SendFilejava

/

/

package client;

import clientwindowWindow;

import javaio;

import javanet;

/

@author 泥鳅

/

public class SendFile {

private Socket socket;

private FileInputStream input;

private DataOutputStream output;

private File file;

public SendFile(String fileName,String ip,int port) throws IOException{

//生成一个 File对象 以获得选择文件长度(大小)

file = new File(fileName);

/ if((int)filelength()>2000000000){

//文件太大就不发送

JOptionPaneshowMessageDialog(null, "文件太大,请重新选择!");

}

else{

/ WindowtextAreaappend("文件大小:"+StringvalueOf(filelength())+"Bytes"+"\n");

socket = new Socket(ip,port);

input = new FileInputStream(fileName);

output = new DataOutputStream(socketgetOutputStream());

byte[] b = new byte[1024];

int length = 0;

while((length = inputread(b)) !=-1){

outputwrite(b,0,length);

Systemoutprintln("发送字节:"+length);

}

// }

WindowtextAreaappend("发送完毕"+"\n");

Systemoutprintln("发送完毕");

inputclose();

outputflush();

outputclose();

}

}

/////////////////////////////////////////////////服务端:

////////////////////////文件一:Windowjava

/

/

package client;

import clientwindowWindow;

import javaio;

import javanet;

/

@author 泥鳅

/

public class SendFile {

private Socket socket;

private FileInputStream input;

private DataOutputStream output;

private File file;

public SendFile(String fileName,String ip,int port) throws IOException{

//生成一个 File对象 以获得选择文件长度(大小)

file = new File(fileName);

/ if((int)filelength()>2000000000){

//文件太大就不发送

JOptionPaneshowMessageDialog(null, "文件太大,请重新选择!");

}

else{

/ WindowtextAreaappend("文件大小:"+StringvalueOf(filelength())+"Bytes"+"\n");

socket = new Socket(ip,port);

input = new FileInputStream(fileName);

output = new DataOutputStream(socketgetOutputStream());

byte[] b = new byte[1024];

int length = 0;

while((length = inputread(b)) !=-1){

outputwrite(b,0,length);

Systemoutprintln("发送字节:"+length);

}

// }

WindowtextAreaappend("发送完毕"+"\n");

Systemoutprintln("发送完毕");

inputclose();

outputflush();

outputclose();

}

}

////////////////////////////////////////文件二:GetFilejava

/

/

package server;

import javanet;

import javaio;

import serverwindowWindow;

/

@author 泥鳅

/

public class GetFile extends Thread{

private ServerSocket server;

private Socket socket;

private FileOutputStream output;

private DataInputStream input;

public GetFile() throws IOException{

}

public void run(){

try{

server = new ServerSocket(6666);

socket = new Socket();

Systemoutprintln("服务器启动");

socket = serveraccept();

byte[] b = new byte[1024];

input = new DataInputStream(socketgetInputStream());

output = new FileOutputStream(StoreFilePATH,true);

int length = 0;

while((length = inputread(b) )!= -1){

outputwrite(b,0,length);

Systemoutprintln("接受成功:"+"接受字节"+length);

}

WindowtextAreaappend("保存完毕!"+"\n");

Systemoutprintln("保存完毕!");

serverclose();

socketclose();

inputclose();

outputclose();

}

catch(IOException e){

Systemoutprintln(egetMessage());

}

}

}

/////////////////////////文件三:StoreFilejava

/

/

package server;

import javaxswingJDialog;

import javaxswingJFileChooser;

import javaioFile;

import javaxswingJOptionPane;

/

@author 泥鳅

/

public class StoreFile {

private JDialog jd;

private JFileChooser chooseFile;

public static String PATH;

public static String FILENAME;

private File fileName;

public StoreFile(){

jd = new JDialog();

chooseFile = new JFileChooser();

if(JFileChooserAPPROVE_OPTION == chooseFileshowSaveDialog(jd)){

String oldstr = chooseFilegetSelectedFile()getPath();

String newStr = changePath(oldstr);

fileName = new File(newStr);

if(fileNameexists()){

JOptionPaneshowMessageDialog(null, "请文件已经存在,输入其他文件名!");

}

else{

//返回路径

PATH = newStr;

//返回文件名

FILENAME = chooseFilegetSelectedFile()getName();

}

/ if(oldstr == null){

JOptionPaneshowMessageDialog(null, "请选择存储文件!");

}

/

}

}

//将路径格式转换成 java所识别的路径格式

public String changePath(String oldstr){

return oldstrreplaceAll("\\\\", "\\\\\\\\");

}

}

朋友我还是告诉你思路先:用流的方法,先获取地址,再用 URLConnection 函数获取输入流,有了输入流,之后把数据弄进输出流里。代码如下

public static void main(String[] args) {

Scanner s=new Scanner(Systemin);//在显示面板上手动输入的函数

int k=0;//配合输出数据用的

Systemoutprintln("请你输入下载地址");

String dz=snextLine()://地址如:http的,把输入的数据放进DZ变量里

URL url=new URL(dz);

URLConnection c=urlopenConnection();//返回一个Connection对象。

InputStream in=cgetInputStream();//读取的输入流。

String fname = urlgetFile()substring(urlgetFile()lastIndexOf("/") + 1);//

"这是一个网站"http://www111com/kk/1mp3",urlgetFile()lastIndexOf("/") + 1;这句意思是到网站最后一个"/",根据上面的网址/1mp3,后面再加一位,结果就是1mp3,它作用是在此字符串中最右边出现的指定子字符串的索引(数字)。

substring这个函数是可以从字符串第几个数字开始并返回一个新的字符串。

这句话的意思是:lastIndexOf("/")+1找到最后一个像"/"字符,然后移一位在哪个位置设一个索引(数字),然后substring是根据这个索引(数字)找到后面那些字符串。

String xxx=fnamesubstring(fname lastIndexOf("")+1);//取文件的后续名(mp3、rmvb)之类

Systemoutprintln("输入文件名:");

String bcwj=snextLine();

FileOutputStream out=new FileOutputStream("D:/m/"+bcwj+""+xxx);

Systemoutprintln("开始下载");

while((k=inread())>=0){

outwrite(k);

outflush();

}

outclose();

inclose();

Systemoutprintln("下载完成");

}

看你的意思,我觉得你可以去网上查查java网络爬虫的实现,就是自动抓取网页内容,并保存

http://blogcsdnnet/binyao02123202/article/details/5725396这里有个链接你可以参考下

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/13493543.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-09-01
下一篇2025-09-01

发表评论

登录后才能评论

评论列表(0条)

    保存