我不知道該如何判斷 但我有一個很笨的方法
如果是一個下載文件 後台會有下面的操作(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的屬性格式。