File upload is a very common function of the site, directly using the servlet to get uploaded files to parse request parameters, more trouble, so the general choice to use
Apache Open source Tools, common-fileupload. This jar pack can be found on the Apache web side, or in the Lib folder of struts,
The ability to upload struts is based on this implementation.
Common-fileupload is dependent on the Common-io
file file
= new file (dir);
If the directory does not exist if
(!file.exists ()) {
///Create directory
file.mkdirs ();
}
return dir;
} catch (Exception e) {
e.printstacktrace ();
return basepath+ "/error";
}
Because the FileUpload plug-in reads the Web file to obtain a imputstream, here, pass in the input stream o
Struts2 upload file based on Apache FileUpload components, after importing FileUpload, small file upload is normal, but upload large files but found that there is a limit of 2M bytes, query struts2 official information to be modified struts.xml to set, Set it to 20M by pretending to adjust it.
The problem should have ended, but found that Glassfish also has the Max Post Size limit, so also modified,
Run fileupload to preview the uploaded image.
See the results:
In the project, create An ASPX page and pull the fileupload control to preview the uploaded image.
In the page_init event, register the onchange client event as the fileupload control. Protected void page_init (Object sender, eventargs E)
{
This. fileupload1.attributes. Add ("onchange
In the process of doing a small project need to use the image upload function, so use a small file upload commons-fileupload components.
The response process is this: in the JSP page through the input tag to get picture file ==> sent to the servlet processing ==>servlet in the validation size, format, etc., and finally write the hard drive and path saved to the Database ==> control page Jump .
Servlet in the processing code I do not mention, and the m
With the help of the FileUpload control in vs2005, the paper uploads the picture file and generates the thumbnail.Implementation process: Choose the image upload success, get already existing server file generation thumbnail, and judge whether it is a picture type of file, this judgment can be modified in the program, this procedure just judged the "image/bmp", "Image/gif", "Image/pjpeg" three different types.The code is as follows:upfile.aspx file
Pr
It uses the symfony framework and jquery to upload images, but the background cannot accept data. Code and error message: {code ...} {code ...} the background code is as follows: {code ...} the error message is: Notice: Undefinedindex: files []. What is the problem? Thank you. It uses the symfony framework and jquery to upload images, but the background cannot accept data. The code and error message are as follows:
$(document).ready(function(){ $("#
Commons-fileupload implements upload -- JAVA
In many Web site applications, users must be provided with the function of uploading documents and materials through browsers, such as uploading personal photos and sharing documents. This function is available in DRP. You need to upload and display the corresponding material images.
The upload function provides excellent support on the browser side. You only need to obtain and save the File Uploaded By the
You will find that after the form of enctype = "multipart/form-Data" is submitted, other parameters cannot be obtained using the traditional request. getparameter in the background.
I personally understand that every parameter is processed in the form of a stream and can be obtained only in a special way.
This document uses commons-fileupload to obtain parameters other than file.
HtmlCode:
Form ID = "Uploadfileform" Action = "Upload
Use of the FileUpload component and Multifile uploadSignificance of File UploadUploading files frequently in projects: uploading images via qq, uploading images through photo albums, sharing data ......Steps for File Upload1. specify the form type as a file upload form.You must specify the form attribute enctype = "multipart/form-data"2. The submission method must be post.Process uploaded files manually// Obtain the file in the form// Obtain the input
Install and use jquery-file-upload step by step1. Install gem
Add the gem of jquery-fileupload-rails and paperclip to gemfile:
gem "jquery-fileupload-rails"gem 'paperclip'2. Add in APP/assets/application. js
//= require jquery-fileupload3. Add in APP/assets/stylesheets/application.css
*= require jquery.fileupload-ui4. Create a picture data table
rails g model Picture avatar:attachmentrake db:migrate
1 File Upload
HTML can upload files to the server via
The jar package used in this example: Commons-fileupload-1.3.2.jar Commons-io-2.5.jar (FileUpload dependent)
Front-end JSP page:
There are several points to note: The Enctype of form form must be "Multipart/form-data". The name attribute must be in the
The service side needs to establish a uploadfileservlet to process the request.The key Dopost
For the code to become more simple, the author used a regular expression to get the file name extension, if the reader is not familiar with the regular expression or have never been contacted, please immediately evil to fill it! After all, it's very important that most programming languages support regular expressions.
As we all know, backstage gets the FileUpload extension method:
String ext = path.getextension (fu. Postedfile.filename);
1. Get the Commons-fileupload component
Download this component in Http://jakarta.apache.org/site/downloads, unzip the file and copy Commoms-fileupload-1.2.jar to the Web-inf/lib directory
2. When using the Commons-fileupload component, another build Commoms-io support is required, which can be found on the http://jakarta.apache.org/site/downloads. Commoms-io.jar
Use the jar package associated with the component:
Commons-fileupload-1.2.2.jar
Commons-io-2.4.jar
Usage:
Create a disk Factory
DiskFileItemFactory factory = new DiskFileItemFactory();
Set File Cache path
File temp = new File(this.getServletContext().getRealPath(“/”) + “temp”) ;factory.setReponsitory(temp) ;
Create processing tool
ServletFileUpload upload = new ServletFileUpload();
Sets the maximum size of the uploaded file.-1 indicates unlimite
In updatepanel, you cannot use fileupload to make up for fileupload. The server cannot find the file to be uploaded.
How to Implement
1: create default. aspx on the home page
Place an updatepanel area of the uploaded attachment in the appropriate location.
Width = "64"/> Protected void button#click (Object sender, eventargs E)
{
String filefullpath = fu_photo.postedfile.filename;
String filename = filefullp
Commons-fileupload New API use, old use, need temp directory, new API directly use stream way as follows checkthatwehaveafileuploadrequestbooleanismultipart =servletfileupload.ismultipartcontent (Request);//createanewfile Uploadhandlerservletfileuploadupload=newservletfileupload ();//Parse Therequestfileitemiteratoriter=upload.getitemiterator (Request);while ( Iter.hasnext ()) {fileitemstreamitem=iter.next (); stringname=item.getfieldname (); inputstr
Ajax is used, and fileupload is not compatible with updatepanel. Fileupload is located between , the controlid here is the ID of the clicked button. solve the problem!
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.