Hi,
I have code Upload file by Servrlet, it ran perfectly, Howver, When I recode(copy this code into Portlet) this Upload into Portlet, it not
word.
Why it not work?
Jav.
You can refer the following my code:
SERVLET:
...
public void init(ServletConfig config) throws ServletException {
super.init(config);
// Read the uploadDir from the servlet parameters
String dirName = config.getInitParameter("uploadDir");
if (dirName == null) {
throw new ServletException("Please supply uploadDir parameter");
}
dir = new File(dirName);
if (! dir.isDirectory()) {
throw new ServletException("Supplied uploadDir " + dirName +
" is invalid");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
out.println("Demo Upload Servlet using MultipartParser");
out.println();
try {
MultipartParser mp = new MultipartParser(request, 10*1024*1024); // 10MB
Part part;
while ((part = mp.readNextPart()) != null) {
String name = part.getName();
if (part.isParam()) {
// it's a parameter part
ParamPart paramPart = (ParamPart) part;
String value = paramPart.getStringValue();
out.println("param: name=" + name + "; value=" + value);
}
else if (part.isFile()) {
// it's a file part
FilePart filePart = (FilePart) part;
String fileName = filePart.getFileName();
if (fileName != null) {
// the part actually contained a file
long size = filePart.writeTo(dir);
out.println("file: name=" + name + "; fileName=" + fileName +
", filePath=" + filePart.getFilePath() +
", contentType=" + filePart.getContentType() +
", size=" + size);
}
else {
// the field did not contain a file
out.println("file: name=" + name + "; EMPTY");
}
out.flush();
}
}
}
catch (IOException lEx) {
this.getServletContext().log(lEx, "error reading or saving file");
}
}
...
=============================================
AND PORTLET:
..
public void init(PortletConfig portletConfig) throws UnavailableException {
log("DemoRequestUploadServlet,INIT");
super.init(portletConfig);
String dirName = portletConfig.getInitParameter("uploadDir");
log("DIRNAME= "+dirName);
if (dirName == null) {
throw new UnavailableException("Please supply uploadDir parameter");
}
dir = new File(dirName);
if (! dir.isDirectory()) {
throw new UnavailableException("Supplied uploadDir " + dirName +
" is invalid");
}
}
public void doView(PortletRequest req, PortletResponse res) throws PortletException, IOException {
log("DemoRequestUploadServlet.doView() call..");
DefaultPortletAction action = new DefaultPortletAction("upload");
PortletURI url = res.createURI();
url.addAction(action);
req.setAttribute("baseURL", url.toString());
getPortletConfig().getContext().include(jspUpload, req, res);
}
public void actionPerformed(ActionEvent event) throws PortletException {
// Log info
log("MyListPortlet.actionPerformed)( call");
PortletContext portletContext = getPortletConfig().getContext();
PortletLog log = portletContext.getLog();
if (log.isDebugEnabled())
log.debug(this.getClass().getName()+".actionPerformed(): Entered");
// Execute the perform action method for the event
PortletRequest request = event.getRequest();
DefaultPortletAction action = (DefaultPortletAction)event.getAction();
if (action != null && action.getName().equalsIgnoreCase("upload")) {
log("HERE");
try {
upload(event.getRequest());
} catch(Exception e) {
throw new PortletException("UPLOAD ERROR: "+e.toString());
}
}
}
public void upload(PortletRequest request) throws Exception {
System.out.println("Demo Upload Servlet using MultipartParser");
System.out.println();
String type1 = request.getHeader("Content-Type");
String type2 = request.getContentType();
int length = request.getContentLength();
ServletInputStream in = request.getInputStream();
log("upload.type1 = "+type1);
log("upload.type2 = "+type2);
log("upload.length = "+length);
log("upload.IN = "+in.toString());
try {
MultipartParser mp = new MultipartParser(request, 10*1024*1024); // 10MB
Part part;
while ((part = mp.readNextPart()) != null) {
String name = part.getName();
if (part.isParam()) {
// it's a parameter part
ParamPart paramPart = (ParamPart) part;
String value = paramPart.getStringValue();
log("param: name=" + name + "; value=" + value);
}
else if (part.isFile()) {
// it's a file part
FilePart filePart = (FilePart) part;
String fileName = filePart.getFileName();
if (fileName != null) {
// the part actually contained a file
long size = filePart.writeTo(dir);
log("file: name=" + name + "; fileName=" + fileName +
", filePath=" + filePart.getFilePath() +
", contentType=" + filePart.getContentType() +
", size=" + size);
}
else {
// the field did not contain a file
log("file: name=" + name + "; EMPTY");
}
}
}
}
catch (IOException lEx) {
this.getServletContext().log(lEx, "error reading or saving file");
}
}