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

时间:2016-01-07 21:16:15

标签: 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="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盘用户和管理员的权限修改为完全控制权,但它确实会影响错误。

1 个答案:

答案 0 :(得分:0)

你可以试试这个

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

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

我希望它可以帮到你! :)