javaweb上传图片到服务器 File cant be saved (1120).该如何解决

javaweb上传图片到服务器 File can't be saved (1120).
异常
com.jspsmart.upload.SmartUploadException: File can't be saved (1120).
at com.jspsmart.upload.File.saveAs(File.java:108)
at com.jspsmart.upload.File.saveAs(File.java:65)
at com.mh.web.servlet.ExpertServlet.regExpert(ExpertServlet.java:259)
at com.mh.web.servlet.ExpertServlet.doPost(ExpertServlet.java:68)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.mh.web.filter.SecurityFilter.doFilter(SecurityFilter.java:63)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.mh.web.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:21)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)


public void regExpert(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
Expert exp = new Expert();
SmartUpload mySmartUpload = new SmartUpload();
String path = request.getContextPath();
// request.getScheme()
String basePath = request.getScheme() + "://"
+ Constants.SERVER_ADDRESS + ":" + request.getServerPort()
+ path;
try {

    mySmartUpload
    .initialize(this.getServletConfig(), request, response); // 设置长传操作的初始化
    mySmartUpload.setMaxFileSize(1024 * 1024 * 1024); // 设置上传文件的大小
    mySmartUpload.upload();
    Files files = mySmartUpload.getFiles(); // 获取上传的文件操作
    com.jspsmart.upload.File singleFile = files.getFile(0); // 获取上传文件的单个文件
    if (!singleFile.isMissing()) { // 判断该文件是否被选择
String fileType = singleFile.getFileName().substring(
singleFile.getFileName().length() - 3);
String filename = System.currentTimeMillis() + "." + fileType;

String realDirectory = request.getSession().getServletContext()
.getRealPath(Constants.REAL_DIRECTORY);
String filedir = realDirectory + "/" + filename; // 以系统时间作为上传文件名称,设置上传文件的完整路径
File file = new File(realDirectory);

if (!file.isDirectory()) { // 如果文件夹不存在就新建
    file.mkdirs();
}
exp.setImage(basePath+Constants.REAL_DIRECTORY + filename);
singleFile.saveAs(filedir); // 执行上传操作  【第259行】

    }

} catch (Exception e) {
    e.printStackTrace();
}
exp.setUsername(mySmartUpload.getRequest().getParameter("username"));
exp.setTruename(mySmartUpload.getRequest().getParameter("truename"));
exp.setPasswd(mySmartUpload.getRequest().getParameter("passwd"));
exp.setRemark(mySmartUpload.getRequest().getParameter("remark"));
exp.setTypeid(Integer.parseInt(mySmartUpload.getRequest().getParameter("typeid")));
boolean flag = ExpertDao.getInstance().regExpert(exp);
if(flag){
response.sendRedirect("/fsb/expertServlet?flag=1");
}
else{
request.getRequestDispatcher("/error/error.jsp").forward(request,response);
}
return;

}
------解决思路----------------------

相关内容推荐