当前位置: 代码迷 >> Java Web开发 >> 用ext实现上传文件的源代码,该怎么处理
  详细解决方案

用ext实现上传文件的源代码,该怎么处理

热度:1246   发布时间:2013-02-25 21:22:14.0
用ext实现上传文件的源代码
求:
用ext+spring实现的图片上传的源代码
跟这边发表提问事,点击“点击上传附件”时出现的对话框一样的效果

------解决方案--------------------------------------------------------
DEMO------EXT+Servlet文件上传2007-12-28 11:11做了一个简单的文件上传,测试文本文件通过,图片不能通过.

大家修改

-----index.jsp-------------

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head> 
<script type="text/javascript" src="js/ext/ext-base.js"></script>
<script type="text/javascript" src="js/ext/ext-all.js"></script>
<script type="text/javascript" src="js/ext/locale/ext-lang-en.js"></script>

<link rel="stylesheet" type="text/css" href="js/ext/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="js/ext/resources/css/xtheme-aero.css" />

</head>
<body>
<script text="text/javascript">
Ext.onReady(function(){
Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'side';
var uploadFile = new Ext.form.Form({
url:'<%=request.getContextPath()%>/servlet/UploadServlet',
labelAlign: 'null',
labelWidth: null,
buttonAlign: 'left',
fileUpload: true,
});
uploadFile.column({width:300, style:null});
uploadFile.add(new Ext.form.TextField({fieldLabel: '选择文件',name: 'myFile',value: '',inputType:'file'}));

uploadFile.addButton('确定保存', function() {
uploadFile.url = '<%=request.getContextPath()%>/servlet/UploadServlet';
uploadFile.action = '<%=request.getContextPath()%>/servlet/UploadServlet';
if (uploadFile.isValid()) {
uploadFile.submit({
params:{action:'submit',flag:'true'},
waitMsg:'Uploading...',
method: 'post',
success:function(form, action ){Ext.MessageBox.alert('Success!','上传成功!');},
failure:function(form, action ){var data = action.result;Ext.MessageBox.alert('Failed!', '上传失败');}});
}
else {
Ext.MessageBox.alert('Errors', '表单有错误,请确认后再提交.');
}
}, uploadFile); 
uploadFile.render('grid-example');
});
</script>
<div id="grid-example"></div>
</body>
</html>
--------------------------servlet--------------------------------

package com.zxg.test.servlet;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.imageio.stream.ImageOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UploadServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public UploadServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BufferedInputStream bis=new BufferedInputStream(request.getInputStream());
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("e:\\test.txt",true));
PrintWriter out=response.getWriter();
if(bis!=null){
int ch;
while((ch=bis.read())!=-1)
{
bos.write(ch);
}
bis.close();
bos.close();
out.write("上传成功");
}
out.write("上传失败");
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
  相关解决方案