日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 转换上传文档_自己编写JAVA环境下的文件上传组件 (转)

發布時間:2025/5/22 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 转换上传文档_自己编写JAVA环境下的文件上传组件 (转) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

客戶端上傳后,端的數據流頭尾部格式如下,這里上傳了一個文檔

我們看看數據流的頭部:

-----------------------------7d22f821706e0Content-Disposition: form-data;

name="filea"; filename="C:工作流管理總體設計.doc"Content-Type:

application/msword

邢唷??......? (注意這里是數據文件開始的地方)

....................................

尾部標志:

-----------------------------7d22f821706e0Content-Disposition: form-data;

name="btnval"

上載-----------------------------7d22f821706e0--

所以去掉頭尾部標志性數據我們就得到上傳的文件數據了。

下面我們看看處理上傳的Bean:uploaean 首先得到初始化Bean得到上下文環境:

public final void initialize(PageContext pageContext)

throws Exception

{

m_application = pageContext.getServletContext();

m_request = (HttpServletRequest)pageContext.getRequest();

m_response = (HttpServletResponse)pageContext.getResponse();

}

將數據流寫到一個BYTES數組中,數組大小就是REQUEST流的大小。

m_totalBytes = m_request.getContentLength();

m_binArray = new byte[m_totalBytes];

for(; totalRead < m_totalBytes; totalRead += readBytes)

try

{

m_request.getInputStream();

readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead);

}

catch(Exception e)

{

System.out.println(" Unable to upload data .");

e.printStackTrace();

}

2。下面就開始處理BYTES數組

以前我見到有網友的作法將m_binArray直接轉化成String,然后利用String的方法去掉標志

位數據

String newstr=new String(m_binArray);

這一方法我試過了,是行不通的,我不知道它們測試過沒有,對于上傳文本文件應該沒問題

如果上傳的是WORD文檔或者圖片等二進制文件,進行轉化成字符串的時候數據就會有丟失,我想

可能是因為編碼的原因造成的,所以不能直接轉換。

正確的方法是將字節數組的每一位轉化成CHAR或判斷ASCII碼值來判斷頭尾標志為數據:

for(; !found && m_currentIndex < m_totalBytes; m_currentIndex++)

{

if(m_binArray[m_currentIndex] == 13)

found = true;

else

m_boundary = m_boundary + (char)m_binArray[m_currentIndex];

}

if(m_currentIndex == 1)

return;

m_currentIndex++;

do

{

if(m_currentIndex >= m_totalBytes)

break;

dataHeader = getDataHeader();

System.out.println(dataHeader);

m_currentIndex = m_currentIndex + 2;

iile = dataHeader.indexOf("filename") > 0;

getDataSection();

if(isFile)

{

System.out.println("-----&gtm_startData:"+m_startData);

System.out.println("-----&gtm_endData:"+m_endData);

try

{

fileout = new FileOutputStream("c:test11.doc");

fileout.write(m_binArray,m_startData,m_endData-m_startData+1);

fileout2 = new FileOutputStream("c:test00.doc");

fileout2.write(m_binArray);

}

catch(Exception e)

{

System.out.println(" Unable to write data to test file .");

e.printStackTrace();

}

finally

{

try

{

fileout.close();

fileout2.close();

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

private String getDataHeader()

{

int start = m_currentIndex;

int end = 0;

int len = 0;

boolean found = false;

while(!found)

if(m_binArray[m_currentIndex] == 13 && m_binArray[m_currentIndex + 2] == 13)

{

found = true;

end = m_currentIndex - 1;

m_currentIndex = m_currentIndex + 2;

}

else

{

m_currentIndex++;

}

String dataHeader = new String(m_binArray, start, (end - start) + 1);

return dataHeader;

}

private void getDataSection()

{

boolean found = false;

String dataHeader = new String();

int searchP= m_currentIndex;

int keyPos = 0;

int boundaryLen = m_boundary.length();

m_startData = m_currentIndex;

m_endData = 0;

do

{

if(searchPos >= m_totalBytes)

break;

if(m_binArray[searchPos] == (byte)m_boundary.charAt(keyPos))

{

if(keyPos == boundaryLen - 1)

{

m_endData = ((searchPos - boundaryLen) + 1) - 3;

break;

}

searchPos++;

keyPos++;

}

else

{

searchPos++;

keyPos = 0;

}

}

while(true);

m_currentIndex = m_endData + boundaryLen + 3;

}

m_currentIndex為當前字節數組的指針位置,先得到HEADER數據,見方法getDataHeader()

然后在得到文件數據開始的指針位置和SIZE,見方法getDataSection()

然后,將數據流寫到文件里:

fileout.write(m_binArray,m_startData,m_endData-m_startData+1);

就得到了完整的數據文件,上面test00.doc 和 test11.doc 是去掉標志數據前后的數據流

分別寫成的文件,大家用二進制查看程序可看出其中的差別。

以上getDataHeader(),getDataSection()是我參考了SMARTUPLOAD的實現原理,

其中分析標志數據位的方法稍有復雜,網友可自己去分析。程序在下測試通過,

上傳WORD文檔和圖片都沒問題。

總結

以上是生活随笔為你收集整理的java 转换上传文档_自己编写JAVA环境下的文件上传组件 (转)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。