java文件上传到服务器 j smartupload
我的学习用的一个web项目我将tomcat放在阿里云服务器上用的jsmartupload本地都可以用移到服务器就用不了不知道是路径问题还是其他的protectedvoid...
我的学习用的一个web项目 我将tomcat放在阿里云服务器上 用的jsmartupload 本地都可以用 移到服务器就用不了 不知道是路径问题还是其他的
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
HttpSession session = request.getSession();
String page="";
SmartUpload mySmartUpload = null;
try {
mySmartUpload = new SmartUpload();
} catch (Exception e) {
e.printStackTrace();
}
String taskNumber=(String)session.getAttribute("tasknumber");
mySmartUpload.initialize(config,request,response);
try {
mySmartUpload.upload();
} catch (SmartUploadException e1) {
e1.printStackTrace();
}
try {
com.jspsmart.upload.File file = mySmartUpload.getFiles().getFile(0);
file.saveAs("http://115.29.52.183:80/FileStorage/"+taskNumber+"task.csv",SAVEAS_AUTO);
com.jspsmart.upload.File file1 = mySmartUpload.getFiles().getFile(1);
file1.saveAs("http://115.29.52.183:80/FileStorage/"+taskNumber+"user.csv",SAVEAS_AUTO);
} catch (Exception e) {
e.printStackTrace();
}
} 展开
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
HttpSession session = request.getSession();
String page="";
SmartUpload mySmartUpload = null;
try {
mySmartUpload = new SmartUpload();
} catch (Exception e) {
e.printStackTrace();
}
String taskNumber=(String)session.getAttribute("tasknumber");
mySmartUpload.initialize(config,request,response);
try {
mySmartUpload.upload();
} catch (SmartUploadException e1) {
e1.printStackTrace();
}
try {
com.jspsmart.upload.File file = mySmartUpload.getFiles().getFile(0);
file.saveAs("http://115.29.52.183:80/FileStorage/"+taskNumber+"task.csv",SAVEAS_AUTO);
com.jspsmart.upload.File file1 = mySmartUpload.getFiles().getFile(1);
file1.saveAs("http://115.29.52.183:80/FileStorage/"+taskNumber+"user.csv",SAVEAS_AUTO);
} catch (Exception e) {
e.printStackTrace();
}
} 展开
2个回答
展开全部
SmartUpload su = new SmartUpload();
JspFactory factory = JspFactory.getDefaultFactory();
PageContext pageContext = null;
JSONObject obj = new JSONObject();
String fileName = UUID.randomUUID().toString();
try {
initFile();
pageContext = factory.getPageContext(this, request, response, "", true, 8192, true);
su.initialize(pageContext);
int size = su.getSize();
if(size>=400){
obj.put("state", false);
obj.put("msg", "图片不能大于400KB");
}else{
su.upload();
Files file = su.getFiles(); //必须在upload后才有值
//此为得到文件的扩展名,getFile(0)为得到唯一的一个上传文件
String ext=file.getFile(0).getFileExt();
String name = fileName+"."+ext;
su.getFiles().getFile(0).saveAs("report/upload/"+name);
WebConstants.REPORT_LOGO_URL="report/upload/"+name;
obj.put("fileName", name);
obj.put("state", true);
obj.put("ext", ext);
}
} catch (Exception e) {
obj.put("state", false);
obj.put("msg",e.toString());
e.printStackTrace();
}
PrintWriter out = response.getWriter();
out.print(obj);
out.flush();
接收到文件后就在tomcat\webapps\项目\report\upload下面
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询