This is a modified version of the code in this post
How-To: Upload and Serve files using Java Servlets on OpenShift. The original post was written by Corey, Red Hat, Inc. OpenShift (PaaS) Senior Product Marketing Manager @ Red Hat.
When I ran Corey's code, I came across few issues. So below code includes some fixes for those issues.
Issues:
1. Corey's code has issues on local server (Local Tomcat 7 running on Windows in my case)
2. Files with space in the file name fails to be served.
Fix:
First of all, open you windows environment variables window and add a new variable for
OPENSHIFT_DATA_DIR
As you can see, I've created a folder
D:\openshift\uploads\ and added it to the windows environment variable OPENSHIFT_DATA_DIR. You can use any folder of your choice. Just make sure that the folder is outside your project folder and git.
Now, change Corey's version of
Uploads.java to look like below. Changes are highlighted in
bold letters.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLDecoder;
import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet(name = "uploads", urlPatterns = { "/uploads/*" })
@MultipartConfig
public class Uploads extends HttpServlet {
private static final long serialVersionUID = 2857847752169838915L;
int BUFFER_LENGTH = 4096;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
for (Part part : request.getParts()) {
InputStream is = request.getPart(part.getName()).getInputStream();
String fileName = getFileName(part);
FileOutputStream os = new FileOutputStream(
System.getenv("OPENSHIFT_DATA_DIR") + fileName);
byte[] bytes = new byte[BUFFER_LENGTH];
int read = 0;
while ((read = is.read(bytes, 0, BUFFER_LENGTH)) != -1) {
os.write(bytes, 0, read);
}
os.flush();
is.close();
os.close();
out.println(fileName + " was uploaded to "
+ System.getenv("OPENSHIFT_DATA_DIR"));
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//Remove extra context path which exists in local Tomcat applications.
String filePath = request.getRequestURI().substring(
request.getContextPath().length());
//Decode url. Fixes issue with files having space within the file name
filePath = URLDecoder.decode(filePath, "UTF-8");
File file = new File(System.getenv("OPENSHIFT_DATA_DIR")
+ filePath.replace("/uploads/", ""));
InputStream input = new FileInputStream(file);
response.setContentLength((int) file.length());
response.setContentType(new MimetypesFileTypeMap().getContentType(file));
OutputStream output = response.getOutputStream();
byte[] bytes = new byte[BUFFER_LENGTH];
int read = 0;
while ((read = input.read(bytes, 0, BUFFER_LENGTH)) != -1) {
output.write(bytes, 0, read);
output.flush();
}
input.close();
output.close();
}
private String getFileName(Part part) {
for (String cd : part.getHeader("content-disposition").split(";")) {
if (cd.trim().startsWith("filename")) {
String filename = cd.substring(cd.indexOf('=') + 1);
//remove extra file path in windows local machine
return filename.substring(filename.lastIndexOf("\\") + 1)
.trim().replace("\"", "");
}
}
return null;
}
}
I've kept the above
Uploads.java file in
src/main/java. Here
java folder is the
source folder and the
Uploads.java file is in the default package.
Folder structure (application created with OpenShift plugin on Eclipse):
Rest of the code and instructions are the same as in Corey's post
How-To: Upload and Serve files using Java Servlets on OpenShift.
Hope this helps. Thanks Corey for your post, which gave the basic understanding of how upload works on OpenShift. Let me know if anyone needs help.
Have a great day!