JspFactory factory = JspFactorygetDefaultFactory();
PageContext pageContext = null;
JSONObject obj = new JSONObject();
String fileName = UUIDrandomUUID()toString();
try {
initFile();
pageContext = factorygetPageContext(this, request, response, "", true, 8192, true);
suinitialize(pageContext);
int size = sugetSize();
if(size>=400){
objput("state", false);
objput("msg", "不能大于400KB");
}else{
suupload();
Files file = sugetFiles(); //必须在upload后才有值
//此为得到文件的扩展名,getFile(0)为得到唯一的一个上传文件
String ext=filegetFile(0)getFileExt();
String name = fileName+""+ext;
sugetFiles()getFile(0)saveAs("report/upload/"+name);
WebConstantsREPORT_LOGO_URL="report/upload/"+name;
objput("fileName", name);
objput("state", true);
objput("ext", ext);
}
} catch (Exception e) {
objput("state", false);
objput("msg",etoString());
eprintStackTrace();
}
PrintWriter out = responsegetWriter();
outprint(obj);
outflush();
接收到文件后就在tomcat\webapps\项目\report\upload下面
下面先介绍上传文件到服务器(多文件上传):
import javaxservlet;
import javaxservlet>FormFile ff = uploadFormgetPic();//先定义一个uploadForm,里有get和set方法
String fileName = ffgetFileName();//文件原名
FileOutputStream fos = null;
//得到文件数据(数组)
byte[] fileData = ffgetFileData();
//得到服务器硬盘文件夹的绝对路径
ServletContext application = thisgetServlet()getServletContext();
String realPath = applicationgetRealPath("/");
fos = new FileOutputStream(realPath+"xxxxlsx");//名称自己起或者使用原文件名
foswrite(fileData);
fosclose();
//上面代码在你的webusermanageraction里
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)