2 baiyun1452 baiyun1452 于 2013.11.06 10:30 提问

【新手跪求大神T_T】FLEX上传java服务器端的时候一直提示路径不对 我都快被整疯了

自己想设计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";
}
}

2个回答

xiangnan129
xiangnan129   Rxr 2013.11.09 12:29
已采纳

具体的问题我也没有看出来,我说一下我的建议,首先你得那个private String uploadPath = "d:\files\"; 你的这个路径是不会存储到服务器上面去的,但是不会影响程序运行。再就是我觉得在windows和linux兼容的情况下,你应该使用\ \来代替"\",因为没有具体跑你的程序,所以你可以试着修改一下这个路径的的问题,看可以不可以。"d"\files\",仅供参考。

xiangnan129
xiangnan129 显示问题,我的意思是用双反斜杠来代替单的斜杠。
大约 4 年之前 回复
ght521
ght521   2013.11.21 18:54

兄弟,是不是安全沙箱问题呢。

baiyun1452
baiyun1452 谢谢回答 我已经解决了
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!