自己想設計flex上傳的功能 所以在網上尋找上傳的例子
但是沒有一個能運行成功的
所有的例子都報這個錯誤:
Error #2044: 未處理的 IOErrorEvent:。 text=Error #2038: 文件 I/O 錯誤。
java文件我用tomcat加載的時候沒問題為什麼還出現這個錯誤呢
web.xml中
url-pattern :/servlet/UploadFile
servlet-name: UploadFile
flex的代碼是這樣的
<?xml version="1.0" encoding="utf-8"?>
layout="vertical" horizontalAlign="left" fontSize="12"
initialize="init()"
viewSourceURL="srcview/index.html">
mx:Script
<![CDATA[
import mx.events.CollectionEvent;
import mx.formatters.NumberFormatter;
import mx.formatters.CurrencyFormatter;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
private var fileRefs: FileReferenceList = new FileReferenceList();
//這個地址是我測試用的服務器地址
private var urlrequest: URLRequest = new URLRequest("http://localhost:8080/abc/servlet/UploadFile");
[Bindable]
private var selectedFiles: ArrayCollection = new ArrayCollection([]);
private var singleThreadFiles: Array = [];
[Bindable]
private var useSingleThread: Boolean = true;
private function init(): void
{
Security.allowDomain("*");
fileRefs.addEventListener(Event.SELECT, fileSelectHandler);
fileRefs.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
fileRefs.addEventListener(Event.COMPLETE, completeHandler);
addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
private function selectFile(): void
{
fileRefs.browse([new FileFilter("Images (*.jpg, .jpeg, *.gif, *.png)", ".jpg;*.jpeg;*.gif;*.png"),
new FileFilter("所有文件(*.*)", "*.*")
]);
}
private function fileSelectHandler(event: Event): void
{
for each (var f: FileReference in fileRefs.fileList)
{
selectedFiles.addItem(f);
}
}
private function uploadFile(): void
{
for each (var f: FileReference in selectedFiles)
{
try
{
f.upload(urlrequest);
}
catch (e: Error)
{
Alert.show(e.message);
}
}
}
private function singleThreadUploadFile(): void
{
//FIFO:逐個從列表中取出,進行同步上傳
if (singleThreadFiles.length > 0)
{
var f: FileReference = singleThreadFiles.pop() as FileReference;
f.addEventListener(Event.COMPLETE, doSingleUploadFileComplete);
f.upload(urlrequest);
}
}
private function doSingleUploadFileComplete(event: Event): void
{
var f: FileReference = event.target as FileReference;
f.removeEventListener(Event.COMPLETE, doSingleUploadFileComplete);
singleThreadUploadFile();
}
private function ioErrorHandler(e:IOErrorEvent): void
{
Alert.show(e.text);
}
private function completeHandler(e: Event): void
{
img.source = e.target.data;
}
private function showImage(e: Event): void
{
var f: FileReference = (e.target as DataGrid).selectedItem as FileReference;
f.addEventListener(Event.COMPLETE, completeHandler);
f.load();
}
public function removeFile(f: FileReference): void
{
var index: int = selectedFiles.getItemIndex(f);
if (index != -1)
selectedFiles.removeItemAt(index);
}
]]>
/mx:Script
mx:VBox
/mx:Box
/mx:HBox
mx:columns
mx:itemRenderer
mx:Component
/mx:Component
/mx:itemRenderer
/mx:DataGridColumn
mx:itemRenderer
mx:Component
mx:Script
<![CDATA[
import flash.profiler.showRedrawRegions;
import mx.controls.Alert;
import mx.controls.ProgressBar;
private function initProgressBar(event: Event): void
{
//使progressbar與file關聯,從而產生進度條
var pb: ProgressBar = event.target as ProgressBar;
pb.label = "%3%%";
pb.setProgress(0, 100);
var f: FileReference = data as FileReference;
//使用閉包方法,更新進度條
f.addEventListener(ProgressEvent.PROGRESS,
function(event: ProgressEvent): void
{
pb.setProgress(event.bytesLoaded, event.bytesTotal);
}
);
f.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,
function (event: DataEvent): void
{
//服務器端一定要返回數據,否則,這個方法就不起作用了
pb.label = event.data;
}
);
}
]]>
/mx:Script
maximum="100" minimum="0" labelPlacement="center" mode="manual"
label="%3%%" textAlign="left"
creationComplete="initProgressBar(event)"/>
mx:click
<![CDATA[
var f: FileReference = data as FileReference;
f.cancel();
]]>
/mx:click
/mx:LinkButton
mx:click
<![CDATA[
var f: FileReference = data as FileReference;
outerDocument.removeFile(f);
]]>
/mx:click
/mx:LinkButton
/mx:HBox
/mx:Component
/mx:itemRenderer
/mx:DataGridColumn
/mx:columns
/mx:DataGrid
/mx:VBox
mx:HBox
mx:click
<![CDATA[
if (useSingleThread)
{
//逐個上傳
singleThreadFiles = selectedFiles.toArray().concat();
singleThreadFiles.reverse();
singleThreadUploadFile();
}
else
{
//多個文件同時上傳
uploadFile();
}
]]>
/mx:click
/mx:Button
change="useSingleThread = !checkboxSingleThread.selected"/>
/mx:HBox
/mx:Application
用在JAVA服務器端的是
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadFile extends HttpServlet {
private static final long serialVersionUID = 5425836142860976977L;
/** //*
* Processes requests for both HTTP GET
and POST
methods.
* @param request servlet request
* @param response servlet response
/
// 定義文件的上傳路徑
private String uploadPath = "d:\files\";
// 限制文件的上傳大小
private int maxPostSize = 100 * 1024 * 1024; //最大100M
public UploadFile() {
super();
}
public void destroy() {
super.destroy();
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Access !");
response.setContentType("text/html;charset=UTF-8");
//保存文件到服務器中
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");
upload.setSizeMax(maxPostSize);
try {
List fileItems = upload.parseRequest(request);
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String name = item.getName();
System.out.println(name);
try {
item.write(new File(uploadPath + name));
response.getWriter().write("上傳成功。");
} catch (Exception e) {
e.printStackTrace();
response.getWriter().write(e.getMessage());
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
response.getWriter().write(e.getMessage());
System.out.println(e.getMessage() + "結束");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/* //*
* Handles the HTTP POST
method.
* @param request servlet request
* @param response servlet response
/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/* //*
* Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
}
具體的問題我也沒有看出來,我說一下我的建議,首先你得那個private String uploadPath = "d:\files\"; 你的這個路徑是不會存儲到服務器上面去的,但是不會影響程序運行。再就是我覺得在windows和linux兼容的情況下,你應該使用\ \來代替"\",因為沒有具體跑你的程序,所以你可以試著修改一下這個路徑的的問題,看可以不可以。"d"\files\",僅供參考。