<script type="text/javascript">
Ext.onReady(function(){
var form = new Ext.form.FormPanel({
labelAlign: 'right',
title: '文件上传',
labelWidth: 50,
frame:true,
fileUpload: true,
url: 'uploadFile?mark='+"",//fileUploadServlet
width: 380,
items: [{
xtype: 'textfield',
fieldLabel: '文本框',
name: 'file',
inputType: 'file'//文件类型
}],
buttons: [{
text: '上传',
method:'post',
handler: function() {
form.getForm().submit({
success: function(form, action){
if(action.result.info=='1'){
Ext.Msg.alert('提示', '上传成功.......');
}
},
failure: function(form, action){
if(action.result.info=='4'){
Ext.Msg.alert('错误', '无法读取上传的文件,请确认该文件没有被破坏!');
}else if(action.result.info=='0'){
Ext.Msg.alert('错误','您上传的文件不存在!请确认路径是否写正确!');
}
}
});
}
}]
});
form.render(document.body);
});
</script>
import javax.servlet.Servlet;
import javax.servlet.ServletInputStream;
import javax.servlet.http.*;
import java.io.*;
public class UploadFile extends HttpServlet implements Servlet {
public ServletInputStream sis = null;// 输入流
private byte[] b = new byte[4096];// 字节流存放数组
private byte[] buff = new byte[1024];
public void doPost(HttpServletRequest request, HttpServletResponse response) {
try {
PrintWriter out = response.getWriter();
String systemPath = request.getRealPath("/");// 当前项目路径
sis = request.getInputStream();
int a = 0;
int k = 0;
String path = "";
while ((a = sis.readLine(b, 0, b.length)) != -1) {
path = new String(b, 0, a);
if ((k = path.indexOf("filename=")) != -1) {
path = path.substring(k + 10);
k = path.indexOf("/"");
path = path.substring(0, k);
int c = path.lastIndexOf("//");
String fileName = path.substring(c + 1);// 文件名
int f = fileName.lastIndexOf(".");
String dat = fileName.substring(f + 1);// 扩展名
if (!dat.equals("raq") && dat != "raq") {// 如果扩展名字不是raq.不允许上传
out.print("{failure:true,info:'5'}");// 5表示扩展名不对
return;
}
File fileReadin = new File(path);// 上传过来的文件
if (!fileReadin.exists()) {// 判断文件是否存在
out.print("{failure:true,info:'0'}");// 0表示文件不存在
return;
} else {
File fileOutput = new File(systemPath + "reportFiles//"
+ fileName);// 输出的文件
UploadFile.copyFile(path, systemPath + "reportFiles//"
+ fileName, response);// 把上传的文件copy给新建的文件
out.print("{success:true,info:'1'}");// 1表示上传成功
return;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) {
doPost(request, response);
}
public static boolean copyFile(File filefrom, File fileto, boolean rewrite,
HttpServletResponse response) {
try {
PrintWriter out = response.getWriter();
if (!filefrom.canRead()) {
out.print("{success:true,info:'4'}");// 4表示无法读取上传文件
return false;
}
FileInputStream fosfrom = new FileInputStream(filefrom);
FileOutputStream fosto = new FileOutputStream(fileto);
byte bt[] = new byte[1024];
int c;
while ((c = fosfrom.read(bt)) > 0) {
fosto.write(bt, 0, c);
}
fosfrom.close();
fosto.close();
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
public static boolean copyFile(String from, String to,
HttpServletResponse response) {
File filefrom = new File(from);
File fileto = new File(to);
return copyFile(filefrom, fileto, true, response);
}
}
不知道杂说.自己看代码吧.