Upload images using Struts
Upload images using Struts
Configure in the struts. xml file:
Package edu. ING. ING_pro_school.school.action; import java. io. bufferedInputStream; import java. io. bufferedOutputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java. io. unsupportedEncodingException; import java.net. URLEncoder; import java. Util. date; import org. apache. struts2.ServletActionContext; public class SetFaceAction {private static final long serialVersionUID = 1L; private static final int BUFFER_SIZE = 16*1024; // page image private File photoFile; // image name private String photoFileFileName; // image type private String photoFileContentType; private String img; public String getImg () {return img;} public void setImg (String img) {this. img = Img;} public File getPhotoFile () {return photoFile;} public void setPhotoFile (File photoFile) {this. photoFile = photoFile;} public String getPhotoFileFileName () {return photoFileFileName;} public void setPhotoFileFileName (String photoFileFileName) {this. photoFileFileName = photoFileFileName;} public String getPhotoFileContentType () {return photoFileContentType;} public void setPhotoFi LeContentType (String photoFileContentType) {this. photoFileContentType = photoFileContentType;}/** get the Runtime Server Directory ** @ return server root directory */public String getSavePath () {// obtain the absolute path of the system from the server. Return ServletActionContext. getServletContext (). getRealPath (/);} public String download () throws UnsupportedEncodingException {String path = getSavePath () + \ UploadImages \ + this. getImg (); System. out. println (path is + path); BufferedInputStream bis = null; BufferedOutputStream bos = null; OutputStream fos = null; InputStream FD = null; String filepath = path; System. out. println (File path + filepath); File u PloadFile = new File (filepath); try {FD = new FileInputStream (uploadFile); bis = new BufferedInputStream (FCM); fos = ServletActionContext. getResponse (). getOutputStream (); bos = new BufferedOutputStream (fos); // This is the key code in the pop-up Download Dialog Box ServletActionContext. getResponse (). setHeader (Content-disposition, attachment; filename = + URLEncoder. encode (path, gbk); int bytesRead = 0; // this location is the same as the uploaded one. I will not say much about it. The input stream is used for first reading and then writing with the output stream, the only difference is that I use buffer input and output stream byte [] buffer = new byte [8192]; while (bytesRead = bis. read (buffer, 0, 8192 ))! =-1) {bos. write (buffer, 0, bytesRead);} bos. flush (); FCM. close (); bis. close (); fos. close (); bos. close ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return null;}/*** upload image * modify * @ return * @ throws Exception */public String setFace () throws Exception {if (null! = This. getPhotoFile () {// obtain the image File = this. getPhotoFile (); // file name = file name + Date photoFileFileName = new Date (). getTime () + photoFileFileName. trim (); String savePath = getSavePath () + \ UploadImages \; // you can determine whether mkdir (savePath) exists in the saved folder ); // generate FileOutputStream fos = new FileOutputStream (savePath + photoFileFileName); System. out. println (savePath + photoFileFileName); // save the file FileInputStream FCM = new FileInputStream (files); byte [] buffer = new byte [BUFFER_SIZE]; int len = 0; while (len = FCM. read (buffer)> 0) {fos. write (buffer, 0, len) ;}} return;}/** determine whether the saved folder exists * Java: http://www.javaweb.cc * @ param path the folder path for saving */public void mkdir (String path) {File file = new File (path); // if there is if (file. exists () {System. out. println (the dir is exits); // if not, create} else {file. mkdir (); System. out. println (have made a dir );}}}