Controller layer:
@Controller
@RequestMapping (value = {"/test/testcontroller"})
Public class TestController extends Basecontroller implements Servletcontextaware {
private ServletContext ServletContext;
@Override
Public void Setservletcontext (ServletContext servletcontext) {
this.servletcontext = ServletContext;
}
/** How to write Data
* Add File information
*
* @param file
* @param request
* @return
*/
@RequestMapping (value = {"/testfileadd.html"}, method = Requestmethod.post)
Public Modelandview Totestfileadd (@RequestParam (value = "Files", required = False) Multipartfile file, HttpServlet Request request) {
map<object, object> param = new Hashmap<object, object> ();
String name = File.getoriginalfilename ();
InputStream stream = null;
String iostr = "";
try {
stream = File.getinputstream ();
//Convert the flow to string
byte[] in = new byte[stream.available ()];
Stream.read (in);
iostr = Hqcodec.hexencode (in);//Invoke tool class
} catch (IOException e) {
e.printstacktrace ();
} finally {
if (null! = Stream) {
try {
stream.close ();
} catch (IOException e) {
e.printstacktrace ();
}
}
}
param.put ("file_name", NAME);
param.put ("FILES", iostr);
param.put ("ID");
try {
Accessservice.insert ("Testfile.insertfile", param);//Call method Insert
} catch (Exception e) {
e.printstacktrace ();
}
return new Modelandview ("Sys/user/test");
}
Write the method
Jump to the Modify File page and write the file to the local E drive
@RequestMapping ("/toeditfiles.html")
Public Modelandview tofiles (Integer ID) {
Map<object, object> param = new Hashmap<object, object> ();
map<string, object> model = new hashmap<string, object> ();
Param.put ("id", id);
try {
Map<object, object> map = Accessservice.queryobject ("Testfile.select", param);
string file_name = (string) map.get ("file_name");
Write CLOB data
Clob o = (Clob) map.get ("FILES");
Reader stream = O.getcharacterstream ();
Char[] C = new char[(int) o.length ()];
Stream.read (c);
string s = new string (c);
Stream.Close ();
byte[] Iobyte = Hqcodec.hexdecode (s);
File File = new file ("E:" +file_name);
OutputStream output = new FileOutputStream (file);
Output.write (Iobyte);
Output.close ();
Put the query results into the model
Model.put ("Testfile", map);
} catch (Exception e) {
E.printstacktrace ();
}
return new Modelandview ("Sys/user/toeditfiles", model);
}
}
//Conversion class
public class Hqcodec {
Static final char[] HEX = new char[] {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ',
' F '};
public static String Hexencode (byte[] buffer) {
if (Buffer.length = = 0) {
Return "";
}
int holder = 0;
char[] chars = new char[buffer.length * 2];
for (int i = 0; i < buffer.length; i++) {
Holder = (Buffer[i] & 0xf0) >> 4;
Chars[i * 2] = Hex[holder];
Holder = Buffer[i] & 0x0f;
chars[(i * 2) + 1] = Hex[holder];
}
return new String (chars);
}
public static byte[] Hexdecode (String hex) {
A null string returns an empty array
if (hex = = NULL | | hex.length () = = 0) {
return new byte[0];
} else if (Hex.length () < 3) {
return new byte[] {(byte) (Integer.parseint (hex, & 0xff)};
}
Adjust accordingly for Odd-length strings
int count = Hex.length ();
int nibble = 0;
if (count% 2! = 0) {
count++;
nibble = 1;
}
byte[] buf = new BYTE[COUNT/2];
char c = 0;
int holder = 0;
int pos = 0;
for (int i = 0; i < buf.length; i++) {
for (int z = 0; z < 2 && pos < Hex.length (); z++) {
c = Hex.charat (pos++);
if (c >= ' A ' && C <= ' F ') {
c-= 55;
} else if (c >= ' 0 ' && C <= ' 9 ') {
C-= 48;
} else if (C >= ' a ' && C <= ' F ') {
c-= 87;
}
if (nibble = = 0) {
Holder = c << 4;
} else {
Holder |= C;
Buf[i] = (byte) holder;
}
nibble = 1-nibble;
}
}
return buf;
}
}
To add files to Clob to the Oracle database