我不知道该如何判断 但我有一个很笨的方法
如果是一个下载文件 后台会有下面的操作(J2EE,其他平台也一样,重点是括号里面的参数)
res.setContentType("application/x-download;charset=gbk");
res.setHeader("Content-Disposition" ,"attachment;filename=test.pdf");
得到链接之后需要访问每个链接 使用socket的输入流获取每个链接的头信息 根据头信息判断是否为pdf文件
但这样有一个bug 有些网站给文件取名字不一定就是以真正文件的后缀名结尾
其次 一些压缩文件里面可能也有pdf文件 这样的pdf文件是搜索不出来的
② JAVA怎么通过socket传输各种类型文件
IO
流的概念 什么叫流?
管道 -
流的分类
字节流 字符流
节点流 过滤流 (包装流 处理流 功能流)
输入流 输出流
InputStream抽象类 所有字节输入流统一的父类
OutputStream抽象类 所有字节输出流统一的父类
FileInputStream节点流 能够连接文件作为节点
int read() 无参read 一次读取一个字节 返回的就是字节数据
int read(byte[] data)*
int read(byte[] data,int off,int len)
FileOutputStream
构造方法:File Stringboolean append
*:输出流连接的文件会被自动创建出来 如果已存在那么替换
*:但是输出流连接的文件夹都不存在 直接Exception
write(int data)
write(byte[] data)
write(byte[] data,int off,int len)*
BufferedInputStream(节点输入流[,缓冲空间大小])
BufferedOutputStream(节点输出流[,缓冲空间大小])
*:它们是过滤流 包装流 处理流 它们只能连接节点流 不能直接连文件
使用它们俩是为了提供缓冲空间 从而大幅度的提高每次读写的
吞吐量 从而提高效率
*:Buffered 意味着有缓冲空间 一定要清空缓冲区
flush();
close();
*:一定注意 你可以使用的依然是read方法来实现读取 write 写出
read() read(byte[] data,int off,int len)
write(int data) write(byte data,int off,int len)
DataInputStream
DataOutputStream
*:它们是过滤刘 包装流 处理流 它们只能连接节点流 不能直接连文件
使用他们俩是为了提供读写基本数据类型内存原型的功能
readBoolean()readInt()readLong()
writeBoolean()writeInt()writeLong()
ObjectInputStream
ObjectOutputStream
*:它们是过滤刘 包装流 处理流 它们只能连接节点流 不能直接连文件
使用他们俩是为了能够提供对象持久化的功能
把Java当中不是基本数据类型的对象类型也能保存到文件中
implements Serializable
一个类型的对象如果想被持久化 那么这个类型必须序列化
而且当中所有的属性页必须实现序列化接口
如果是个集合概念 那么不但这个类型需要序列化
当中存放的任何一个元素都需要序列化
readObject()writeObject()
*:如果一个属性不需要参与持久化 那么可以直接把属性定义
为 transient 短暂的 转瞬即逝的
Reader抽象类 所有字符输入流统一的父类
Writer抽象类 所有字符输出流统一的父类
FileReader
int read() 无参read 一次读取一个字符 返回的就是字符数据
int read(char[] data)
int read(char[] data,int off,int len)
FileWriter
*:输出流连接的文件会被自动创建出来 如果已存在那么替换
*:但是输出流连接的文件夹都不存在 直接Exception
write(int data)
write(char[] data)
write(char[] data,int off,int len);
过滤流 包装流 处理流
BufferedReader
String readLine() 一次读取一行
字符串不能返回-1 返回null代表读取结束
BufferedWriter
write(String str) 写出字符串的方法
newLine() 写出一个换行标识
PrintStreamSystem.out
PrintWriter
太有魅力了 太强大了
1.可以连接字符流也可以连接字节流
2.可以当做节点流也可以当做过滤流
3.能够指定自动清空缓冲 (流,true) autoFlush
4.可以指定字符集 字符编码
5.一个println() = write()+newLine()
InputStreamReader
OutputStreamWriter
将原本的字节流包装转换成字符流
RandomAccessFilesetLength();
Socket
服务器 ServerSocket ss = new ServerSocket(7777);
while(true){
Socket s = ss.accept()
new XxxThread(s).start();
}
客户端 Socket socket = new Socket(ip,port);
getInputStream();
getOutputStream();
③ 什么是java socket
事实上网络编程简单的理解就是两台计算机相互通讯数据而已,对于程序员而言,去掌握一种编程接口并使用一种编程模型相对就会显得简单的多了,Java SDK提供一些相对简单的Api来完成这些工作。Socket就是其中之一,对于Java而言,这些Api存在与java.net 这个包里面,因此只要导入这个包就可以准备网络编程了。
网络编程的基本模型就是客户机到服务器模型,简单的说就是两个进程之间相互通讯,然后其中一个必须提供一个固定的位置,而另一个则只需要知道这个固定的位置。并去建立两者之间的联系,然后完成数据的通讯就可以了,这里提供固定位置的通常称为服务器,而建立联系的通常叫做客户端,基于这个简单的模型,就可以进入网络编程啦。
Java对这个模型的支持有很多种Api,而这里我只想介绍有关Socket的编程接口,对于Java而言已经简化了Socket的编程接口。首先我们来讨论有关提供固定位置的服务方是如何建立的。Java提供了ServerSocket来对其进行支持.事实上当你创建该类的一个实力对象并提供一个端口资源你就建立了一个固定位置可以让其他计算机来访问你,ServerSocket server=new ServerSocket(6789);这里稍微要注意的是端口的分配必须是唯一的。因为端口是为了唯一标识每台计算机唯一服务的,另外端口号是从0~65535之间的,前1024个端口已经被Tcp/Ip 作为保留端口,因此你所分配的端口只能是1024个之后的。好了,我们有了固定位置.现在所需要的就是一根连接线了.该连接线由客户方首先提出要求。因此Java同样提供了一个Socket对象来对其进行支持,只要客户方创建一个Socket的实例对象进行支持就可以了。Socket client
=new Socket(InetAddress.getLocalHost(),5678);客户机必须知道有关服务器的IP地址,对于着一点Java也提供了一个相关的类InetAddress 该对象的实例必须通过它的静态方法来提供,它的静态方法主要提供了得到本机IP 和通过名字或IP直接得到InetAddress的方法。
上面的方法基本可以建立一条连线让两台计算机相互交流了,可是数据是如何传输的呢?事实上I/O操作总是和网络编程息息相关的。因为底层的网络是继续数据的,除非远程调用,处理问题的核心在执行上,否则数据的交互还是依赖于IO操作的,所以你也必须导入java.io这个包.java的IO操作也不复杂,它提供了针对于字节流和Unicode的读者和写者,然后也提供了一个缓冲用于数据的读写。
在网络搜索里输入java socket会有N多的结果给你答案。
④ 基于Java Socket实现文件传输
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Client1 extends JFrame
{
private JTextField enter,Iptext;
private JTextArea display;
private JLabel Iplabel;
private JPanel upPanel;
JButton clientMessageButton,connectButton,DisConnectButton;
ObjectOutputStream output;
ObjectInputStream input;
Socket socket1;
String message=" ";
public Client1()
{
super("客户端");
Container c=getContentPane();
clientMessageButton=new JButton("发送");
c.add(clientMessageButton,BorderLayout.EAST);
clientMessageButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{sendData(enter.getText());<br/>}});
DisConnectButton=new JButton("断开连接");
DisConnectButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{DisConnect();<br/>}});
Iptext=new JTextField(10);
Iptext.setText("");
Iptext.setEnabled(true);
Iptext.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{connect();<br/>}});
Iplabel=new JLabel("服务器IP:");
connectButton=new JButton("连接");
connectButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{connect();<br/>}});
upPanel=new JPanel();
upPanel.add(connectButton);
upPanel.add(DisConnectButton);
upPanel.add(Iplabel);
upPanel.add(Iptext);
enter=new JTextField();
enter.setEnabled(true);
enter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{sendData(enter.getText());<br/>}});
c.add(enter,BorderLayout.SOUTH);
c.add(upPanel,BorderLayout.NORTH);
display=new JTextArea();
c.add(new JScrollPane(display),BorderLayout.CENTER);
setSize(300,450);
show();
}
public void connect()
{
try
{
display.setText("准备连接...\n");
String s=Iptext.getText();
socket1=new Socket(InetAddress.getByName(s),8888);
display.append("连接到: "+socket1.getInetAddress().getHostName());
display.append("\n主机IP为:"+socket1.getInetAddress().toString());
output=new ObjectOutputStream(socket1.getOutputStream());
output.flush();
do{
try{
message=(String)input.readObject();
display.append("\n"+message);
}
catch(IOException e)
{
display.append("\n无法获得信息");
}
catch(ClassNotFoundException e)
{}
}while(!message.equals("end"));
display.append("\n关闭连接");
output.close();
input.close();
socket1.close();
}
catch(EOFException eof)
{
System.out.println(eof);
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void DisConnect()
{
try
{
display.append("\n关闭连接");
output.close();
input.close();
socket1.close();
}
catch(Exception e)
{}
}
private void sendData(String s)
{
try
{
message=s;
output.writeObject("客户消息: "+s);
display.append("\n客户:"+s);
output.flush();
enter.setText(" ");
}
catch(Exception e)
{
display.append("\n发生错误");
}
}
public static void main(String args[])//throws NullPointerException
{
Client1 app=new Client1();
app.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{System.exit(0);}});
//app.connect();
}
}
//服务端 2007-12-30 17:33
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Server1 extends JFrame
{
private JTextArea display;
private JTextField enter;
JButton sysMessageButton;
JButton stopMessageButton,startServerButton;
JPanel upPanel;
ServerSocket server;
Socket socket1;
ObjectOutputStream output;
ObjectInputStream input;
public Server1()
{
super("服务器端");
Container c=getContentPane();
enter=new JTextField();
setSize(50,20);
upPanel=new JPanel();
sysMessageButton=new JButton("发送");
c.add(sysMessageButton,BorderLayout.EAST);
setSize(50,20);
stopMessageButton=new JButton("断开连接");
c.add(stopMessageButton,BorderLayout.WEST);
stopMessageButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
stopService();}});
⑤ JAVA socket通信
publicclassClient{
publicstaticvoidmain(String[]args){
Strings=null;
Socketmysocket;
DataInputStreamin=null;
DataOutputStreamout=null;
try{
mysocket=newSocket("127.0.0.1",4331);
in=newDataInputStream(mysocket.getInputStream());
out=newDataOutputStream(mysocket.getOutputStream());
for(intk=1;k<100;k=k+2){
out.writeUTF(""+k);
s=in.readUTF();
System.out.println("客户收到"+s);
Thread.sleep(500);
}
}catch(Exceptione){
System.out.println("服务器已断开"+e);
}
}
}
publicclassServer{
publicstaticvoidmain(String[]args){
ServerSocketserver=null;
Socketyou=null;
Strings=null;
DataOutputStreamout=null;
DataInputStreamin=null;
try{
server=newServerSocket(4331);
}catch(Exceptione){
System.out.println(e);
}
try{
System.out.println("等待客户呼叫");
you=server.accept();
out=newDataOutputStream(you.getOutputStream());
in=newDataInputStream(you.getInputStream());
while(true){
s=in.readUTF();
intm=Integer.parseInt(s);
out.writeUTF("你好,我是服务器");
out.writeUTF("你说的数乘2后是:"+2*m);
System.out.println("服务器收到:"+s);
Thread.sleep(500);
}
}catch(Exceptione){
System.out.println("客户端已断开"+e);
}
}
}
很简单的服务器客户端程序
⑥ C++通过socket 传送pdf问题,接收端是java
C++和java字节序列应该是不同的吧。。。
⑦ 技术问题:关于JAVA的SOCKET
socket只是java针对网络方面的东西,跟flash没有什么必然联系,一般来说大概流程是这样:
用户访问页面---页面调用内嵌的java代码实现逻辑判断、计算等动态功能---这时如果需要网络连接,则调用socket部分代码远程连接(服务器,或别的pc)---再将远程的数据(文字、图片、音频、视频、flash等)读取到本地(通常以字节流形式读取)---前台页面通过html代码将得到的数据表现在页面上。
所以你需要的是两方面:socket连接和字节流读取。
不过抱歉手头没有现成代码,建议楼主搜一下socket和java IO系统,每部分的代码都不多
⑧ 关于用JAVA的SOCKET传输文件
点对点传输文件
/*
import java.io.*;
import java.net.*;
import java.util.*;
*/
private HttpURLConnection connection;//存储连接
private int downsize = -1;//下载文件大小,初始值为-1
private int downed = 0;//文加已下载大小,初始值为0
private RandomAccessFile savefile;//记录下载信息存储文件
private URL fileurl;//记录要下载文件的地址
private DataInputStream fileStream;//记录下载的数据流
try{
/*开始创建下载的存储文件,并初始化值*/
File tempfileobject = new File("h:\\webwork-2.1.7.zip");
if(!tempfileobject.exists()){
/*文件不存在则建立*/
tempfileobject.createNewFile();
}
savefile = new RandomAccessFile(tempfileobject,"rw");
/*建立连接*/
fileurl = new URL("https://webwork.dev.java.net/files/documents/693/9723/webwork-2.1.7.zip");
connection = (HttpURLConnection)fileurl.openConnection();
connection.setRequestProperty("Range","byte="+this.downed+"-");
this.downsize = connection.getContentLength();
//System.out.println(connection.getContentLength());
new Thread(this).start();
}
catch(Exception e){
System.out.println(e.toString());
System.out.println("构建器错误");
System.exit(0);
}
public void run(){
/*开始下载文件,以下测试非断点续传,下载的文件存在问题*/
try{
System.out.println("begin!");
Date begintime = new Date();
begintime.setTime(new Date().getTime());
byte[] filebyte;
int onecelen;
//System.out.println(this.connection.getInputStream().getClass().getName());
this.fileStream = new DataInputStream(
new BufferedInputStream(
this.connection.getInputStream()));
System.out.println("size = " + this.downsize);
while(this.downsize != this.downed){
if(this.downsize - this.downed > 262144){//设置为最大256KB的缓存
filebyte = new byte[262144];
onecelen = 262144;
}
else{
filebyte = new byte[this.downsize - this.downed];
onecelen = this.downsize - this.downed;
}
onecelen = this.fileStream.read(filebyte,0,onecelen);
this.savefile.write(filebyte,0,onecelen);
this.downed += onecelen;
System.out.println(this.downed);
}
this.savefile.close();
System.out.println("end!");
System.out.println(begintime.getTime());
System.out.println(new Date().getTime());
System.out.println(begintime.getTime() - new Date().getTime());
}
catch(Exception e){
System.out.println(e.toString());
System.out.println("run()方法有问题!");
}
}
/***
//FileClient.java
import java.io.*;
import java.net.*;
public class FileClient {
public static void main(String[] args) throws Exception {
//使用本地文件系统接受网络数据并存为新文件
File file = new File("d:\\fmd.doc");
file.createNewFile();
RandomAccessFile raf = new RandomAccessFile(file, "rw");
// 通过Socket连接文件服务器
Socket server = new Socket(InetAddress.getLocalHost(), 3318);
//创建网络接受流接受服务器文件数据
InputStream netIn = server.getInputStream();
InputStream in = new DataInputStream(new BufferedInputStream(netIn));
//创建缓冲区缓冲网络数据
byte[] buf = new byte[2048];
int num = in.read(buf);
while (num != (-1)) {//是否读完所有数据
raf.write(buf, 0, num);//将数据写往文件
raf.skipBytes(num);//顺序写文件字节
num = in.read(buf);//继续从网络中读取文件
}
in.close();
raf.close();
}
}
//FileServer.java
import java.io.*;
import java.util.*;
import java.net.*;
public class FileServer {
public static void main(String[] args) throws Exception {
//创建文件流用来读取文件中的数据
File file = new File("d:\\系统特点.doc");
FileInputStream fos = new FileInputStream(file);
//创建网络服务器接受客户请求
ServerSocket ss = new ServerSocket(8801);
Socket client = ss.accept();
//创建网络输出流并提供数据包装器
OutputStream netOut = client.getOutputStream();
OutputStream doc = new DataOutputStream(
new BufferedOutputStream(netOut));
//创建文件读取缓冲区
byte[] buf = new byte[2048];
int num = fos.read(buf);
while (num != (-1)) {//是否读完文件
doc.write(buf, 0, num);//把文件数据写出网络缓冲区
doc.flush();//刷新缓冲区把数据写往客户端
num = fos.read(buf);//继续从文件中读取数据
}
fos.close();
doc.close();
}
}
*/
⑨ 如何用纯java代码实现word转pdf
1. 需要用的软件
OpenOffice 下载地址http://www.openoffice.org/
JodConverter 下载地址http://sourceforge.net/projects/jodconverter/files/JODConverter/
2.启动OpenOffice的服务
安装完openoffice,安装服务
cdC:Program Files (x86)OpenOffice 4program
执行
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
查看是否安装成功,查看端口对应的pid
netstat -ano|findstr "8100"
查看pid对应的服务程序名
tasklist|findstr "pid值"
3.将JodConverter相关的jar包添加到项目中
4. 下面是实现代码
/**
*将Office文档转换为PDF.运行该函数需要用到OpenOffice,OpenOffice下载地址为
*http://www.openoffice.org/
*
*<pre>
*方法示例:
*StringsourcePath="F:\office\source.doc";
*StringdestFile="F:\pdf\dest.pdf";
*Converter.office2PDF(sourcePath,destFile);
*</pre>
*
*@paramsourceFile
*源文件,绝对路径.可以是Office2003-2007全部格式的文档,Office2010的没测试.包括.doc,
*.docx,.xls,.xlsx,.ppt,.pptx等.示例:F:\office\source.doc
*@paramdestFile
*目标文件.绝对路径.示例:F:\pdf\dest.pdf
*@return操作成功与否的提示信息.如果返回-1,表示找不到源文件,或url.properties配置错误;如果返回0,
*则表示操作成功;返回1,则表示转换失败
*/
publicstaticintoffice2PDF(StringsourceFile,StringdestFile){
try{
FileinputFile=newFile(sourceFile);
if(!inputFile.exists()){
return-1;//找不到源文件,则返回-1
}
//如果目标路径不存在,则新建该路径
FileoutputFile=newFile(destFile);
if(!outputFile.getParentFile().exists()){
outputFile.getParentFile().mkdirs();
}
//connecttoanOpenOffice.orginstancerunningonport8100
=newSocketOpenOfficeConnection(
"127.0.0.1",8100);
connection.connect();
//convert
DocumentConverterconverter=(
connection);
converter.convert(inputFile,outputFile);
//closetheconnection
connection.disconnect();
return0;
}catch(FileNotFoundExceptione){
e.printStackTrace();
return-1;
}catch(ConnectExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
return1;
}
⑩ 关于 java Socket 编程
Struts2的配置文件中返回类型你改了吗???result的type值如下:
chain:将一个action的执行与另外一个配置好的action串连起来。用第一个action的getter方法和第二个action的setter方法来完成action之间属性的复制。
dispatcher:渲染JSP。这是默认的结果类型,如果在action配置中没有配置其他的结果类型,它就会被使用。
freemarker:渲染Freemarker模板。
httpheader:返回HTTP头和用户定义的值。
redirect:重定向到任意的URL。
redirect-action:重定向到配置好的action。可以用来提供post完成以后的重定向功能。
stream:将数据流返回给浏览器。可以用来把数据注入PDF,Microsoft Work,图像或其他数据中。
velocity:渲染 Velocity模板。
xslt:使用XSLT来转换已执行完毕的action的属性格式。