/**
* This class takes the UploadForm and retrieves the text value
* and file attributes and puts them in the request for the display.jsp
* page to display them
*
* @author Mike Schachter
* @version $Revision: 1.8 $ $Date: 2003/02/28 02:18:23 $
*/
public class UploadAction extends Action
{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
if (form instanceof UploadForm) {
int SIZE_OF_ARRYLIST = 2;
//this line is here for when the input page is upload-utf8.jsp,
//it sets the correct character encoding for the response
String encoding = request.getCharacterEncoding();
if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
{
response.setContentType("text/html; charset=utf-8");
}
UploadForm theForm = (UploadForm) form;
//retrieve the text data
String text = theForm.getTheText();
//retrieve the query string value
String queryValue = theForm.getQueryParam();
//retrieve the file representation
FormFile[] file = new FormFile[SIZE_OF_ARRYLIST];
file[0]=theForm.getFile1();
file[1]= theForm.getfile2();
//retrieve the file name
for (int i =0;i<SIZE_OF_ARRYLIST;i++){
String fileName= file[i].getFileName();
//retrieve the content type
String contentType = file[i].getContentType();
try {
//retrieve the file data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file[i].getInputStream();
if (!writeFile) {
//only write files out that are less than 1MB
if (file[i].getFileSize() < (4*1024000)) {
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
baos.write(buffer, 0, bytesRead);
}
data = new String(baos.toByteArray());
}
else {
data = new String("The file is greater than 4MB, " +
" and has not been written to stream." +
" File Size: " + file[i].getFileSize() + " bytes. This is a" +
" limitation of this particular web application, hard-coded" +
" in org.apache.struts.webapp.upload.UploadAction");
}
}
else {
//write the file to the file specified
OutputStream bos = new FileOutputStream(theForm.getFilePath());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
data = "The file has been written to \"" + theForm.getFilePath() + "\"";
}
//close the stream
stream.close();
}
catch (FileNotFoundException fnfe) {
return null;
}
catch (IOException ioe) {
return null;
}
//place the data into the request for retrieval from display.jsp
request.setAttribute("text", text);
request.setAttribute("queryValue", queryValue);
request.setAttribute("fileName"+i, fileName);
request.setAttribute("contentType", contentType);
request.setAttribute("size", size);
request.setAttribute("data", data);
}
//destroy the temporary file created
file.destroy();
//return a forward to display.jsp
return mapping.findForward("display");
}
//this shouldn't happen in this example
return null;
}
}
上面的程序发错了,不过也是一个很好的vb实现的方式,遇到问题多动动脑子,懒人!
<script language=VBScript>
Function bytes2BSTR(vIn)
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
</script>
<script language=javascript>
var xmlhttp ;
var ado_stream ;
var mFileName, mPartStart, mPartID, mPartEnd ;
var SendCount ;
var BlockSize ;
var Breaked ;
var aryFiles ;
function SendData()
{
if (SendCount > 0)
{
var dom = new ActiveXObject("msxml2.DOMDocument"); // 发送的xml文件
dom.async = false;
dom.resolveExternals = false;