java 转换上传文档_自己编写JAVA环境下的文件上传组件 (转)
客戶端上傳后,端的數(shù)據(jù)流頭尾部格式如下,這里上傳了一個文檔
我們看看數(shù)據(jù)流的頭部:
-----------------------------7d22f821706e0Content-Disposition: form-data;
name="filea"; filename="C:工作流管理總體設計.doc"Content-Type:
application/msword
邢唷??......? (注意這里是數(shù)據(jù)文件開始的地方)
....................................
尾部標志:
-----------------------------7d22f821706e0Content-Disposition: form-data;
name="btnval"
上載-----------------------------7d22f821706e0--
所以去掉頭尾部標志性數(shù)據(jù)我們就得到上傳的文件數(shù)據(jù)了。
下面我們看看處理上傳的Bean:uploaean 首先得到初始化Bean得到上下文環(huán)境:
public final void initialize(PageContext pageContext)
throws Exception
{
m_application = pageContext.getServletContext();
m_request = (HttpServletRequest)pageContext.getRequest();
m_response = (HttpServletResponse)pageContext.getResponse();
}
將數(shù)據(jù)流寫到一個BYTES數(shù)組中,數(shù)組大小就是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數(shù)組
以前我見到有網(wǎng)友的作法將m_binArray直接轉化成String,然后利用String的方法去掉標志
位數(shù)據(jù)
String newstr=new String(m_binArray);
這一方法我試過了,是行不通的,我不知道它們測試過沒有,對于上傳文本文件應該沒問題
如果上傳的是WORD文檔或者圖片等二進制文件,進行轉化成字符串的時候數(shù)據(jù)就會有丟失,我想
可能是因為編碼的原因造成的,所以不能直接轉換。
正確的方法是將字節(jié)數(shù)組的每一位轉化成CHAR或判斷ASCII碼值來判斷頭尾標志為數(shù)據(jù):
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("----->m_startData:"+m_startData);
System.out.println("----->m_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為當前字節(jié)數(shù)組的指針位置,先得到HEADER數(shù)據(jù),見方法getDataHeader()
然后在得到文件數(shù)據(jù)開始的指針位置和SIZE,見方法getDataSection()
然后,將數(shù)據(jù)流寫到文件里:
fileout.write(m_binArray,m_startData,m_endData-m_startData+1);
就得到了完整的數(shù)據(jù)文件,上面test00.doc 和 test11.doc 是去掉標志數(shù)據(jù)前后的數(shù)據(jù)流
分別寫成的文件,大家用二進制查看程序可看出其中的差別。
以上getDataHeader(),getDataSection()是我參考了SMARTUPLOAD的實現(xiàn)原理,
其中分析標志數(shù)據(jù)位的方法稍有復雜,網(wǎng)友可自己去分析。程序在下測試通過,
上傳WORD文檔和圖片都沒問題。
總結
以上是生活随笔為你收集整理的java 转换上传文档_自己编写JAVA环境下的文件上传组件 (转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java里程序控制流程_Java语言中的
- 下一篇: locks java_java中Lock