• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Upload/download file

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I newbie Portlet, I have questions about upload/download files.
Do WebSphere Portlet API supply some methods to upload/download files?
Or I must be coding as something that Servlet support?.
Please help me.
Viet.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
}

}
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic