File Upload, File Upload java
I believe that those who have Web development experience are familiar with file upload. But how can I upload files on my website? I think it will be difficult for many people. I am one of them. Recently, due to the development needs, I summarized the principles of file uploading on the Web end. I will share with you here.
Main Code of the jsp page:
<Body>
The following describes the upload methods.
Upload a single file:
SmartUpload smart = new SmartUpload();
smart.initialize(this.getServletConfig(),request,response); try {smart.upload();smart.save("images"); } catch (SmartUploadException e) { // TODO Auto-generated catch block e.printStackTrace();}
File rename:
SmartUpload smart = new SmartUpload (); smart. initialize (this. getServletConfig (), request, response); try {smart. upload (); String imgname = smart. getRequest (). getParameter ("imgname"); String absPath = this. getServletContext (). getRealPath ("/images"); // obtain the file storage path System. out. println (absPath); String ext = smart. getFiles (). getFile (0 ). getFileExt (); // get the File suffix String fileName = absPath + File. separator + imgname + ". "+ ext; // storage name of the generated file System. out. println ("File name =" + fileName); smart. getFiles (). getFile (0 ). saveAs (fileName);} catch (SmartUploadException e) {// TODO Auto-generated catch blocke. printStackTrace ();}
Automatically generated file name:
SmartUpload smart = new SmartUpload();smart.initialize(this.getServletConfig(),request,response);String disFile = "";try {smart.upload();//String imgname = smart.getRequest().getParameter("imgname");IPTimeStamp ipts = new IPTimeStamp();String imgname = ipts.getIPTimestamp();String absPath = this.getServletContext().getRealPath("/images");System.out.println(absPath);String ext = smart.getFiles().getFile(0).getFileExt();String fileName = absPath+File.separator+imgname+"."+ext;System.out.println("File name="+fileName);smart.getFiles().getFile(0).saveAs(fileName);disFile = imgname+"."+ext;} catch (SmartUploadException e) {// TODO Auto-generated catch blocke.printStackTrace();}
Code for generating random file names:
Package cn.edu. hpu. util; import java. text. simpleDateFormat; import java. util. date; import java. util. random; public class IPTimeStamp {private String ip; public IPTimeStamp () {} public IPTimeStamp (String ip) {this. ip = ip;} public String getIPTimestamp () {StringBuffer buffer = new StringBuffer (); if (ip! = Null) {String [] digits = ip. split ("\\. "); for (String s: digits) {buffer. append (s) ;}}// time SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMddHHmmssSSS"); String time = sdf. format (new Date (); buffer. append (time); Random random = new Random (); for (int I = 0; I <4; I ++) {buffer. append (random. nextInt (10);} return buffer. toString ();} public static void main (String [] args) {IPTimeStamp ipts = new IPTimeStamp ("192.168.19.121"); System. out. println (ipts. getIPTimestamp ());}}
Multifile upload:
SmartUpload smart = new SmartUpload (); smart. initialize (this. getServletConfig (), request, response); try {smart. upload (); for (int I = 0; I <smart. getFiles (). getCount (); I ++) {// String imgname = smart. getRequest (). getParameter ("imgname"); IPTimeStamp ipts = new IPTimeStamp (); String imgname = ipts. getIPTimestamp (); String absPath = this. getServletContext (). getRealPath ("/images"); // file storage path String ext = smart. getFiles (). getFile (I ). getFileExt (); // get the File suffix String fileName = absPath + File. separator + imgname + ". "+ ext; // The name System used to generate the storage of the uploaded file. out. println ("File name =" + fileName); smart. getFiles (). getFile (I ). saveAs (fileName) ;}} catch (SmartUploadException e) {// TODO Auto-generated catch blocke. printStackTrace ();}
I believe that you will understand how to complete file upload. (If any error occurs, please correct it. Thank you)
Jsp file upload and download
1. jsp page
<S: form action = "fileAction" namespace = "/file" method = "POST" enctype = "multipart/form-data">
<! -- Name is the parameter name corresponding to the background -->
<S: file name = "files" label = "file1"> </s: file>
<S: file name = "files" label = "file2"> </s: file>
<S: file name = "files" label = "file3"> </s: file>
<S: submit value = "submit" id = "submitBut"> </s: submit>
</S: form>
2. Action
// File files, String filesFileName, String filesContentType can be used for uploading a single File
// The name must be the same as the name in jsp (get, set must be generated for all three variables)
Private File [] files;
// Start with File [] variable name
Private String [] filesFileName;
// Start with File [] variable name
Private String [] filesContentType;
Private ServletContext servletContext;
// The File Upload method called by Action
Public String execute (){
ServletContext servletContext = ServletActionContext. getServletContext ();
String dataDir = servletContext. getRealPath ("/file/upload ");
System. out. println (dataDir );
For (int I = 0; I <files. length; I ++ ){
File saveFile = new File (dataDir, filesFileName [I]);
Files [I]. renameTo (saveFile );
}
Return "success ";
}
3. Configure the Temporary Folder for uploading files (configured in struts. xml)
<Constant name = "struts. multipart. saveDir" value = "c:/temp"/>
File Download
1. Download url (to Action)
<A href = & q ...... remaining full text>
PHP multi-File Upload
<Input type = "file" name = "file1"> <br>
<Input type = "file" name = "file2"> <br>
<Input type = "file" name = "file3"> <br>
<Input type = "file" name = "file4"> <br>
Change
<Input type = "file" name = "file []"> <br>
<Input type = "file" name = "file []"> <br>
<Input type = "file" name = "file []"> <br>
<Input type = "file" name = "file []"> <br>