1) org.apache.log4j.PropertyConfigurator.configure(filepath);
or
2) -Dlog4j.configuration=filepath
or (Web App의 경우)
3)WEB-INF/classes 밑에 log4j.properties
BLOG ARTICLE 개발/Java | 11 ARTICLE FOUND
- 2009.12.11 log4 properties 경로설정.
- 2008.06.30 Java - Multipart Data 대충 샘플
- 2008.04.21 WOL Magic Packet Using Java
- 2008.04.21 Using JFileChooser
- 2007.10.02 Java 스레드/동기화
At Spring
public ModelAndView fileUpload(HttpServletRequest request, HttpServletResponse response)
throws Exception{
MultipartRequest multipartReq = (MultipartRequest)request;
MultipartFile multipartFile = multipartReq.getFile("multipartFile");
String savePath = getServletContext().getRealPath("/")+File.separator+"destDir";
if(multipartFile.isEmpty())
{
response.setHeader(UPLOAD_RESPONSE, "1");
response.setHeader(UPLOAD_DESC, "Empty File");
return null;
}
try
{
File dir = new File(savePath);
if(!dir.exists()) dir.mkdir();
String fileName = savePath + File.separator + multipartFile.getOriginalFilename();
multipartFile.transferTo(new File(fileName));
}catch(Exception e){
response.setHeader(UPLOAD_RESPONSE, "1");
response.setHeader(UPLOAD_DESC, e.getMessage());
return null;
}
response.setHeader(UPLOAD_RESPONSE, "0");
response.setHeader(UPLOAD_DESC, "Success");
return null;
}
At .. Normal Servlet
try{
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
System.out.println("fieldName : "+name);
InputStream stream = item.openStream();
if (item.isFormField()) {
System.out.println("Form field " + name + " with value "
+ Streams.asString(stream) + " detected.");
} else {
System.out.println("File field " + name + " with file name "
+ item.getName() + " detected.");
byte[] ar = new byte[1024];
String dir = "destDir";
System.out.println("item name : "+item.getName());
String fname = null;
if(item.getName().lastIndexOf("\\")+1 <= 0)
fname = item.getName().substring(item.getName().lastIndexOf("/")+1);
else
fname = item.getName().substring(item.getName().lastIndexOf("\\")+1);
if(new File(dir+fname).exists())
fname = dir+"_"+fname;
else
fname = dir+fname;
System.out.println("save as : "+fname);
FileOutputStream fout = new FileOutputStream(fname);
int read = 0;
for(;;){
read = stream.read(ar);
if(read < 0)
{
fout.close();
break;
}
fout.write(ar,0,read);
}
}
}
}catch(Exception e){
//handle exception
}
public static void sendWOLMagicPacket(String addrMac, String addrBroadcast, int port)
throws UnknownHostException, SocketException,
IOException, IllegalArgumentException{
//packet[0~5] = 0xFF
//packet[6~90] = mac[0]mac[1]mac[2]mac[3]mac[4]mac[5]mac[0]mac[1]mac[2]...
byte[] mac = new byte[6];
String[] octet = addrMac.split("(\\:|\\-)");
if (octet.length != 6) {
throw new IllegalArgumentException("Invalid MAC address.");
}
for (int i = 0; i < 6; i++) {
mac[i] = (byte) Integer.parseInt(octet[i], 16);
}
byte[] packet = new byte[6 + 16 * mac.length];
for (int i = 0; i < 6; i++)
packet[i] = (byte) 0xff;
for (int i = 6; i < packet.length; i += mac.length)
System.arraycopy(mac, 0, packet, i, mac.length);
InetAddress group = InetAddress.getByName(addrBroadcast);
DatagramSocket s = null;
try
{
s = new DatagramSocket();
DatagramPacket dgram = new DatagramPacket(packet, packet.length, group, port);
s.send(dgram);
}finally{
if( s != null)
s.close();
}
}
public class FileSelector {
public static File showOpenDlg(Component parent, String[] exts){
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileFilterImpl(exts));
chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
return chooser.getSelectedFile();
}
return null;
}
public static File showSaveDlg(Component parent, String[] exts, String displayName){
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileFilterImpl(exts));
chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
chooser.setSelectedFile(new File(displayName));
int returnVal = chooser.showSaveDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
return chooser.getSelectedFile();
}
return null;
}
}
public class FileFilterImpl extends FileFilter{
public String[] exts;
public FileFilterImpl(String[] exts){
this.exts = exts;
}
public boolean accept(File f)
{
if (f.isDirectory()) {
return true;
}
String extension = getExtension(f);
if(extension == null)
return false;
for(int i=0; i < exts.length; i++){
if(extension.toLowerCase().equals(exts[i]))
return true;
}
return false;
}
public String getDescription(){
StringBuffer sb = new StringBuffer();
for(int i=0; i < exts.length; i++)
{
if(i == (exts.length-1))
sb.append(exts[i]);
else
sb.append(exts[i] + ", ");
}
return sb.toString()+" File Selection";
}
public String getExtension(File f) {
String ext = null;
String s = f.getName();
int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1) {
ext = s.substring(i+1).toLowerCase();
}
return ext;
}
public String getTypeDescription(File f) {
String extension = getExtension(f);
String type = null;
if (extension != null) {
for(int i=0; i < exts.length; i++){
if(exts[i].toLowerCase().equals(extension))
return extension.toUpperCase() + " File";
}
}
return type;
}
}