我的 servlet 托管在免费试用 apache 服务器中,用于测试目的。因此,当我尝试将 jpeg 图像上传到该服务器中的图像文件夹时,它会出现此错误:
java.security.AccessControlException: access denied ("java.io.FilePermission" "/usr/local/shared/tomcat/dilis/temp/upload_e1bed5d1_54a9_45e0_b1ae_26732a1441fc_00000059.tmp" "write")
java.security.AccessControlContext.checkPermission(AccessControlContext.java:366)
java.security.AccessController.checkPermission(AccessController.java:555)
java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
java.lang.SecurityManager.checkWrite(SecurityManager.java:979)
java.io.FileOutputStream.<init>(FileOutputStream.java:203)
java.io.FileOutputStream.<init>(FileOutputStream.java:165)
org.apache.commons.io.output.DeferredFileOutputStream.thresholdReached(DeferredFileOutputStream.java:178)
org.apache.commons.io.output.ThresholdingOutputStream.checkThreshold(ThresholdingOutputStream.java:224)
org.apache.commons.io.output.ThresholdingOutputStream.write(ThresholdingOutputStream.java:128)
org.apache.commons.fileupload.util.Streams.copy(Streams.java:108)
org.apache.commons.fileupload.util.Streams.copy(Streams.java:70)
org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:347)
org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:115)
sanin.lands.web.Mobile_slt.doPost(Mobile_slt.java:137)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
sun.reflect.GeneratedMethodAccessor32.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
java.security.AccessController.doPrivileged(Native Method)
javax.security.auth.Subject.doAsPrivileged(Subject.java:536)
org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)
我应该如何将临时目录更改为我可以访问的位置或任何其他方式?
这是我的 servlet 代码:
if (!((FilenameUtils.getName(item.getName()) == "") ||
(FilenameUtils.getName(item.getName()).isEmpty()))) {
String rootPath = System.getProperty("catalina.home");
File theDir = new File(rootPath + "\\webapps\\images\\" + max_id);
// if the directory does not exist, create it
if (!theDir.exists()) {
boolean result = theDir.mkdir();
if (result) {
System.out.println("DIR created");
}
}
String fieldname = item.getFieldName();
InputStream filecontent = item.getInputStream();
File f = new File(rootPath + "\\webapps\\images\\" + max_id + "\\" + item.getName());
try {
// item.write(file);
item.write(f);
} catch (Exception ex) {
Logger.getLogger(Mobile_slt.class.getName()).log(Level.SEVERE, null, ex);
}