我正在尝试创建文件上传系统。但是,我卡住了,无法解决问题。 我的core.cjl
(ns hiccup-templating.core
(:require [compojure.core :refer [defroutes GET ANY POST]]
[compojure.route :as route]
[compojure.handler :as handler]
[ring.adapter.jetty :as jetty]
[hiccup-templating.views.layout :as layout]
[hiccup-templating.views.contents :as contents]
(ring.middleware [multipart-params :as mp])
(clojure.contrib [duck-streams :as ds])))
(defn upload-file
[file]
(ds/copy (file :tempfile) (ds/file-str (str (rand-int 30) ".jpg")))
(layout/application "Home" (contents/succes)))
(defroutes public-routes
(GET "/" [] (layout/application "Home" (contents/index)))
(mp/wrap-multipart-params
(POST "/file" {params :params} (upload-file (get params "file")))))
(def application (handler/site public-routes))
(defn -main []
(let [port (Integer/parseInt (or (System/getenv "PORT") "8080"))]
(jetty/run-jetty application {:port port :join? false})))
所以我有这样的错误。
2017-04-25 17:00:57.934:WARN:oejs.AbstractHttpConnection:/file
java.lang.NullPointerException
at hiccup_templating.core$upload_file.invoke(core.clj:14)
at hiccup_templating.core$fn__3152.invoke(core.clj:20)
at compojure.core$make_route$fn__489.invoke(core.clj:94)
at compojure.core$if_route$fn__473.invoke(core.clj:40)
at compojure.core$if_method$fn__466.invoke(core.clj:25)
at ring.middleware.multipart_params$wrap_multipart_params$fn__947.invoke(multipart_params.clj:107)
at compojure.core$routing$fn__495.invoke(core.clj:107)
at clojure.core$some.invoke(core.clj:2443)
at compojure.core$routing.doInvoke(core.clj:107)
at clojure.lang.RestFn.applyTo(RestFn.java:139)
at clojure.core$apply.invoke(core.clj:619)
at compojure.core$routes$fn__499.invoke(core.clj:112)
at ring.middleware.keyword_params$wrap_keyword_params$fn__854.invoke(keyword_params.clj:32)
at ring.middleware.nested_params$wrap_nested_params$fn__903.invoke(nested_params.clj:70)
at ring.middleware.params$wrap_params$fn__820.invoke(params.clj:58)
at ring.middleware.multipart_params$wrap_multipart_params$fn__947.invoke(multipart_params.clj:107)
at ring.middleware.flash$wrap_flash$fn__2119.invoke(flash.clj:31)
at ring.middleware.session$wrap_session$fn__2099.invoke(session.clj:85)
at ring.adapter.jetty$proxy_handler$fn__2209.invoke(jetty.clj:18)
at ring.adapter.jetty.proxy$org.eclipse.jetty.server.handler.AbstractHandler$0.handle(Unknown Source)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:363)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:483)
at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:931)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:992)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:856)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Thread.java:745)
如果有人给我如何理解我的问题,我会很高兴。