I get following error when I try to upload file to Google Cloud Storage through servlet using Google Client Library. The wierd part is file uploading process run okay when I do at local development server, error happened in deployed version of the app
java.lang.IllegalStateException: No multipart config for servlet
and this is my uploading servlet
import com.google.cloud.storage.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@MultipartConfig()
public class GCSAccessServlet extends HttpServlet {
private static final String BUCKET_NAME = System.getenv("bucketName");
private static Storage storage = null;
@Override
public void init() {
storage = StorageOptions.getDefaultInstance().getService();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (BUCKET_NAME.equals("") || BUCKET_NAME == null) {
System.out.println("Bucket is not found (null / empty name)");
}
Bucket bucket = storage.get(BUCKET_NAME);
req.setAttribute("bucketName", bucket.getName());
resp.setContentType("text/html");
RequestDispatcher jsp = req
.getRequestDispatcher("upload.jsp");
jsp.forward(req, resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException,
ServletException {
final Part filePart = req.getPart("file");
final String fileName = filePart.getSubmittedFileName();
// Modify access list to allow all users with link to read file
List<Acl> acls = new ArrayList<>();
acls.add(Acl.of(Acl.User.ofAllUsers(), Acl.Role.READER));
// the inputstream is closed by default, so we don't need to close it here
Blob blob =
storage.create(
BlobInfo.newBuilder(BUCKET_NAME, fileName).setAcl(acls).build(),
filePart.getInputStream());
// return the public download link
resp.getWriter().print(blob.getMediaLink());
}
}
I couldn't find explanation about this problem.
Thanks.