swfupload 的文件上传程序 java 上传方式 !

上段时间看了很多的关于swfupload 的资料!很多人写的让我没有办法很简单的运用 swfupload ,对于这个swfupload 很多人都说好,但是具体的例子很少,有空我就弄了一下,真的很简单!

[img]http://dl.iteye.com/upload/attachment/215168/f67aee29-c845-31ab-8fc3-5f641cc0553b.jpg[/img]

最主要的就是javascript 在页面中如何调用 servlet


<script type="text/javascript">
var swfu;

window.onload = function() {
var settings = {
flash_url : "<%=imgpath %>/swfupload/swfupload.swf",
upload_url: "<%=imgpath %>/servlet/UploadServlet",
file_size_limit : "1000 MB",
file_types : "*.*",
file_types_description : "All Files",
file_upload_limit :2,
file_queue_limit : 0,
custom_settings : {
progressTarget : "fsUploadProgress",
cancelButtonId : "btnCancel"
},
debug: false,

// Button settings
button_image_url: "<%=imgpath %>/swfupload/images/TestImageNoText_65x29.png",
button_width: "65",
button_height: "29",
button_placeholder_id: "spanButtonPlaceHolder",
button_text: '<span class="theFont">上传</span>',
button_text_style: ".theFont { font-size: 16; }",
button_text_left_padding: 12,
button_text_top_padding: 3,

// The event handler functions are defined in handlers.js
file_queued_handler : fileQueued,
file_queue_error_handler : fileQueueError,
file_dialog_complete_handler : fileDialogComplete,
upload_start_handler : uploadStart,
upload_progress_handler : uploadProgress,
upload_error_handler : uploadError,
upload_success_handler : uploadSuccess,
upload_complete_handler : uploadComplete,
queue_complete_handler : queueComplete // Queue plugin event
};

swfu = new SWFUpload(settings);

};
</script>


主要的servlet 的上传文件代码!

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
FileItemFactory factory = new DefaultFileItemFactory();
FileUpload upload = new FileUpload(factory);
Iterator items;
try {
items = upload.parseRequest(request).iterator();
while (items.hasNext()) {
FileItem item = (FileItem) items.next();
if (!item.isFormField()) {
String name = item.getName();
String filetype = name.substring(name.lastIndexOf("."));
String filename = System.currentTimeMillis()+filetype;
// System.out.println(filename+"--"+new String(name.getBytes("GBK"),"UTF-8"));
String realpath = request.getRealPath("/upload");
String path = realpath + File.separatorChar + filename;
File file = new File(path);
if (!file.exists()) {
File file1 = new File(realpath);
file1.mkdirs();
file.createNewFile();
}else{

file.createNewFile();
}
item.write(file);
response.setContentType("text/html");
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
out.print("<font size='2'>上传的文件为:" + name + "<br>");
out.print("保存的地址为:" + path + "</font>");

}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}


好了,不多介绍了!需要的话直接下载我的 项目吧!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值