繁体   English   中英

在Java Spring中将文件上传到tomcat服务器时拒绝访问

[英]access is denied while uploading file to tomcat server in java spring

这是我的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>

这是我的角度js控制器:fileupload.js

adminApp.controller('fileUploadCtrl', ['$scope', 'fileUpload', function ($scope, fileUpload) {
var baseUrl = window.location.origin;
var base = window.location.protocol;
var url = window.location.host;
if (!baseUrl) {
    baseUrl = base + "//" + url;
}
var basePathName = window.location.pathname;
var adminUrl = baseUrl + basePathName;

var productImageRestUrl = 'REST/files/uploadFile';

$scope.uploadFile = function () {
    var file = $scope.myFile;
    console.log('file is ' + JSON.stringify(file));
    var uploadUrl = adminUrl + productImageRestUrl;
    fileUpload.uploadFileToUrl(file, uploadUrl);
};
}]);

这是我的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(访问被拒绝)

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

我调试了代码,该代码表明它加载了data.jpg(无论图像文件名是什么),但是它从未保存到服务器位置。

我试图将c驱动器用户和管理员的权限固定为完全控制,但这确实会导致错误。

你可以试试这个

File serverFile = new File(dir.getAbsolutePath()
                        + File.separator, file.getOriginalFilename());

它将保存与上载文件(名称,扩展名)完全相同的文件。

希望对您有帮助! :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM