Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在JavaSpring中将文件上载到tomcat服务器时,访问被拒绝_Javascript_Java_Angularjs_Spring Mvc_Tomcat - Fatal编程技术网

Javascript 在JavaSpring中将文件上载到tomcat服务器时,访问被拒绝

Javascript 在JavaSpring中将文件上载到tomcat服务器时,访问被拒绝,javascript,java,angularjs,spring-mvc,tomcat,Javascript,Java,Angularjs,Spring Mvc,Tomcat,这是我的html文件:upload.html <div ng-controller="fileUploadCtrl" class="allOrder"> <br /> <br /> <form ng-submit="uploadFile()" enctype="multipart/form-data"> <div class="form-group"> <label for="image" class="

这是我的html文件:upload.html

<div ng-controller="fileUploadCtrl" class="allOrder">
<br /> <br />

<form ng-submit="uploadFile()" enctype="multipart/form-data">
    <div class="form-group">
        <label for="image" class="col-lg-2 control-label">Item Image:</label>

        <div class="col-lg-6">
            <input type="file" class="form-control" file-Model="myFile"
                id="image"> <br />

            <button type="submit" class="btn btn-primary">Upload Image</button>
        </div>
    </div>
</form>
这是我的java控制器:FileUploadController.java

public class FileUploadController {

private static final String SERVER_UPLOAD_LOCATION_FOLDER = System
        .getProperty("catalina.home") + "/kukus/images/";

private static final Logger logger = LoggerFactory
        .getLogger(FileUploadController.class);

/**
 * Upload single file using Spring Controller
 */
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public @ResponseBody
String uploadFileHandler(@RequestParam("file") MultipartFile file) {
    System.out.println("File"+file);

    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();

            // Creating the directory to store file
            System.out.println("Catalina base" +System.getProperty("catalina.base"));
            String rootPath = SERVER_UPLOAD_LOCATION_FOLDER;
            System.out.println("rootPath" +rootPath);
            File dir = new File(rootPath + File.separator + "tmpFiles");
            System.out.println("File Dir" +dir);
            if (!dir.exists())
                dir.mkdirs();

            // Create the file on server
            File serverFile = new File(dir.getAbsolutePath()
                    + File.separator);
            System.out.println("AbsolutePath" +dir.getAbsolutePath()+ "ServerFile" +serverFile.getAbsolutePath());
            BufferedOutputStream stream = new BufferedOutputStream(
                    new FileOutputStream(serverFile));
            stream.write(bytes);
            stream.close();

            logger.info("Server File Location="
                    + serverFile.getAbsolutePath());

            return "You successfully uploaded file=";
        } catch (Exception e) {
            return "You failed to upload " + " => " + e.getMessage();
        }
    } else {
        return "You failed to upload " + " because the file was empty.";
    }
}
每次我试图上传图像时,它都会在消息框中显示一条消息。。c:/apachetomcat/kukus/images/tempFiles(访问被拒绝)

在控制台中,输出为-file为()

我调试了代码,它显示它加载data.jpg(不管图像文件名是什么),但它从未保存到服务器位置

我试图将c驱动器-用户和管理员的权限修复为完全控制,但它确实会影响错误。

您可以尝试此操作

File serverFile = new File(dir.getAbsolutePath()
                        + File.separator, file.getOriginalFilename());
它将保存与上传文件完全相同的文件(名称、扩展名)


我希望它能帮助你!:)

您是否检查了有效使用的文件名?打印它们并尝试手动创建它们我想
fileserverfile=newfile(dir.getAbsolutePath()+File.separator)表示目录,而不是文件。@Marged我试图给出另一个路径,即b:/uploads/。。要保留映像以进行测试,但仍然存在错误,访问被拒绝。。。。它与spring security有关吗?我正在服务器@JohannesJander上创建一个文件。我不明白为什么访问被拒绝。Johannes的意思是:也许你试图写入目录而不是文件。因此,我要求输出文件名
File serverFile = new File(dir.getAbsolutePath()
                        + File.separator, file.getOriginalFilename());