hi all ..
actually my requirment is to read a user selected file ( csv) .. do some operation and write the results into a new file to the server ...
can any one help me how to do this ..
any help will be greatly appreciated ...
thanx in advance ...
hi all ..
actually my requirment is to read a user selected file ( csv) .. do some operation and write the results into a new file to the server ...
can any one help me how to do this ..
any help will be greatly appreciated ...
thanx in advance ...
This is really a general programming question rather than a Spring question. You might want to look for file manipulation libraries.
thanks for the reply ...
actually i think some uploading of file is required before programming with libraries ..
my quiestion is whether i need to physically copy the file to server side before doing some file operations on it ...
or what to do in such a situation ...?
sorry for my bad english
thanx again
<!—test.jsp >
<FORM name='person' method="POST" enctype="multipart/form-data" >
<table>
<tr>
<th width='20%' align='right'>Import File</th>
<td>
<input type="file" name="importFile">
</td></tr>
</table>
</form>
/**
* TestForm.java
*/
import org.apache.commons.fileupload.* ;
public class TestForm extends SimpleFormController {
/** Method used to search for owners renders View depending on how many are found */
protected ModelAndView onSubmit(
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
throws Exception {
// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload();
// Parse the request
List /* FileItem */ items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
logger.info("importFile name=" + item.getFieldName());
if("importFile".equals(item.getFieldName())){
logger.info("importFile=" + item.getName());
readFileContents(item);
break;
}
}
return new ModelAndView(new RedirectView("home.htm"));
}
/**
* read the content of import file
*
* @throws IOException
*/
public void readFileContents(FileItem item ) throws IOException {
//File file = new File(fileName);
String thisLine;
StringBuffer content = new StringBuffer();
String filecontents = "";
//
//logger.info("file exist .." + fileName);
InputStream fin = item.getInputStream();
//JDK1.1+
BufferedReader myInput = new BufferedReader
(new InputStreamReader(fin));
// JDK1.02
// DataInputStream myInput = new DataInputStream(fin);
while ((thisLine = myInput.readLine()) != null) {
//thisLine = doStringReplace(thisLine);
logger.info("thisLine =" + thisLine);
content.append(thisLine);
content.append("\n");
}
fin.close();
//logger.info("file content =\n" + content);
}
}
I have a form that combines input type=file and type=text elements. When I try to do typical request.getParameter calls, I get nulls back on items that work when the form enctype is default...
i got the same problem. i useOriginally Posted by cglenn
fileitem.getString()
to get the values
for complex form and database processing, i prefer Struts forms and direct jdbc satatement