An example of a spring upload file file,

Source: Internet
Author: User
Tags readline

/**

*  Class Name: Uploadtest class Description: created by: Zhang Created: March 13, 2015 4:20:57 person modified: Zhang

* Modified: March 13, 2015 4:20:57 Modify notes:
*
* @version
*
*/
@Controller
Public class Uploadtest {
@RequestMapping (value = "Upfile", method = Requestmethod.post)
Public void Upfile (@RequestParam ("name") String name,
@RequestParam ("file") multipartfile file, HttpServletRequest request) {
String dir = null;
if (!file.isempty ()) {
ServletContext sc = request.getsession (). Getservletcontext ();//Get the path to the project
dir = Sc.getrealpath ("/upload");//Set file to save directory
//

SaveFile01 (file, dir);
SaveFile (file, dir);
}

Testinputtostring (dir);
Testinputsream (dir);

}

/**
* Save upload file Spring format multipartfile with Multipartfile inside the Transferto method
*
* @param file
* @param dir
*/
private void SaveFile01 (multipartfile file, String dir) {
String filename = File.getoriginalfilename (); Get the file name when uploading

File TargetFile = new file (dir, filename);

if (!targetfile.exists ()) {
Targetfile.mkdirs ();

}

try {
File.transferto (targetfile);
} catch (Exception e) {

}
System.out.println ("Upload over." + filename);
}

/**
* @param file
* @param dir
* Store Multipartfile types of files with Dataoutpuestream,
*/
private void SaveFile (multipartfile file, String dir) {
/*************************************/
DataInputStream is = null;
FileOutputStream fos = null;
try {
InputStream in = File.getinputstream ();
is = new DataInputStream (in);
} catch (IOException e) {
//TODO auto-generated catch block
e.printstacktrace ();
}
file fil = new file (dir, File.getoriginalfilename ());
if (!fil.exists ()) {//If file does not exist, create file
fil.mkdirs ();
}
try {
fos = new FileOutputStream (fil);

dataoutputstream dos = new DataOutputStream (FOS);
int data1;
stringbuffer buffer = new StringBuffer ();
while ((data1 = Is.read (by))! =-1) {

buffer.append (data1);
//printstream.write (data1);
}
" catch (Exception e) {
" Span style= "FONT-SIZE:14PT;" >//TODO auto-generated Catch block
e.printstacktrace ();
finally {
try {
if (is! = null) {

Is.close ();
}
if (fos! = null) {
Fos.close ();

}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
/*************************************/
}

/**
* read out the output stream of the file and convert it to a character type to print out
*/

private void Testinputtostring (String dir) {
bufferedreader br = null;
fileinputstream FIP = null;
inputstreamreader ISR = null;
try {
FIP = new FileInputStream (new File (dir + "/new text document. txt"));

string str;
/*
* use ReadLine to print text
*/
ISR = new InputStreamReader (FIP);//Turn bytes into a character stream
BR = new BufferedReader (ISR);//use a character stream buffer to increase efficiency
while ((str = Br.readline ())! = null) {
system.out.println (str);

"catch (Exception e) {
//TODO Auto-generated Catch block
e.printstacktrace ();
finally {
if (BR! = null) {
try {
isr.close ();
br.close ();
"catch (IOException e) {
//TODO Auto-generated Catch block
e.printstacktrace ();
}
}
}

/*
* Use bytes to output the input stream's file Bytearrayoutputsream is an array to hold byte data is written to a byte
Array Buffers automatically grow as data is written continuously
*/
public void Testinputsream (String dir) {

FileInputStream FIP = null;
Bytearrayoutputstream ba = null;
DataInputStream os = null;
try {
FIP = new FileInputStream (New File (dir + "/new text document. txt"));
ba = new Bytearrayoutputstream ();
OS = new DataInputStream (FIP);
byte[] b = new byte[1024];
int rs;
While ((rs = Os.read (b))! =-1) {//reads data from the input stream into byte Group B
Ba.write (b, 0, RS);//write Method B is the data written, 0 is the offset, RS is the size of the write data
}
System.out.println (ba.tostring ());
} catch (Exception e) {
//TODO auto-generated catch block
e.printstacktrace ();
} finally {
if (BA! = null) {//closes the input stream, freeing the resource
try {
ba.close ();
os.close ();
ba.close ();
} catch (IOException e) {
//TODO auto-generated catch block
e.printstacktrace ();
}}}}}

/***************************************/

2. Configuration Files spring4-servlet.xml


<!--automatically scans the controller bean, converts the annotated class to bean--
<mvc:annotation-driven/>
<context:component-scan base-package= "Com.glass.control"/>

<!--resolution of the name of the model view, and the model view name is added before and after the request
<bean
Class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix= "/" p:suffix= ". jsp" >
<property name= "order" value= "0"/>
</bean>

<!--support uploading files--
<bean id= "Multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

An example of a spring upload file file,

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.